6

In Clang or LLVM, how to properly declare an external variable that refers to a CPU register? For example, I would like to express the following declarations (as in https://github.com/mit-pdos/xv6-public/blob/master/proc.h#L27):

extern struct cpu *cpu asm("%gs:0");       // &cpus[cpunum()]
extern struct proc *proc asm("%gs:4");     // cpus[cpunum()].proc

It seems the code above is compiled, but not linked in an expected way. I am porting xv6 to Rust, and as a stepping stone I am porting it to Clang 3.8.0 (instead of GCC). But during the build, the linker issued the following error message:

ld -m    elf_i386 -T kernel.ld -o kernel entry.o bio.o console.o exec.o file.o fs.o ide.o ioapic.o kalloc.o kbd.o lapic.o log.o main.o mp.o picirq.o pipe.o proc.o sleeplock.o spinlock.o string.o swtch.o syscall.o sysfile.o sysproc.o timer.o trapasm.o trap.o uart.o vectors.o vm.o  -b binary initcode entryother
console.o: In function `panic':
.../console.c:114: undefined reference to `%gs:0'
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jeehoon Kang
  • 311
  • 1
  • 13
  • 3
    There some discussion of this [here](http://stackoverflow.com/a/39581910/2189500). In particular, using asm like this is considered an undocumented [hack](https://gcc.gnu.org/ml/gcc/2015-08/msg00075.html). Perhaps you could delete this declaration, and replace all the errors that appear with a function to read the value. – David Wohlferd Nov 23 '16 at 04:08

0 Answers0