0

I'm trying to build an IDT in x86 assembly but have problems storing function addresses in the data section.

Here's my code:

.text
.globl handler0
.type handler0, @function
.align 2
handler0:
pushl $(0)
jmp _alltraps
.data
.hword (((handler0) >> 16) & 0xFFFF)
.hword 0x8F00
.hword 0x0008
.hword ((handler0) & 0xFFFF)

Assembler messages:

kern/trapentry.S:71: Error: invalid operands (.text and *ABS* sections) for `>>'
kern/trapentry.S:74: Error: invalid operands (.text and *ABS* sections) for `&'

It's required to store the higher and lower bits of function handler0 separately. What is the proper way to achieve this in the assembly?

ZRnQ
  • 81
  • 3
  • 1
    @500-InternalServerError The syntax is correct (read the GNU as manual), it's just that these operations are not supported on relocatable symbols. – fuz Feb 20 '23 at 11:56
  • This is not going to work, even if the syntax worked. You'll need to learn how to deal with segmentation. It's not just doing arithmetic to split the linear address into segment and offset. – fuz Feb 20 '23 at 11:57
  • @fuz Isn't it possible for the assembler to know the virtual address of `handler0` at compile time? I have tried `.word handler0` and it works. Besides, the processor is running in 32-bit protection mode at this point, so segmentation should not be a problem. – ZRnQ Feb 20 '23 at 12:18
  • 3
    There is no “compile time” as no compilation happens. At assembly time, the assembler does not know the virtual address of `handler0`. When you write `.word handler0`, the assembler places a *relocation* at that location, telling the linker to late patch in the correct address. Relocations permit simple additions and subtractions of constants, but that's about it. No shifts or other complex arithmetic. – fuz Feb 20 '23 at 13:29
  • Anyway, two general solutions obtain: (a) do the arithmetic in a linker script where it happens after layout and (b) build the IDT at runtime. – fuz Feb 20 '23 at 17:53

0 Answers0