0

I can declare static data regions like so:

.data

var: .byte 0x00, 0x01, ...

But when I try to access them from within .text, I get a compilation error:

lea rax, var # or $var, or [var]

relocation R_X86_64_32S against .data can not be used when making a PIE object; recompile with -fPIC

How do I get the address of static data I declared in the data section?

heroxav
  • 1,387
  • 1
  • 22
  • 65
  • 2
    The ModRM can only encode 32-bit displacements, but PIE executables (default option when using gcc, in most distribution) are not guaranteed to be loaded below 4GiB. However EX executables are, you can tell gcc not to generate a PIE with `-fno-pie` or you can use RIP relative addressing (with NASM it is `lea rax, [REL var]`). Fun fact: using `-fPIC` won't do anything, that message assumes gcc is generating the code. There is a canonical question about this but I don't remember which one it is, try searching the error and it should pop up. – Margaret Bloom Jul 17 '19 at 09:09
  • @MargaretBloom is this AT&T syntax? I've been using Intel syntax thus far, what would be the equivalent of `REL` there? Sadly, I'm also getting the compilation error with `-fno-pie`. – heroxav Jul 17 '19 at 11:01
  • 1
    `lea var(%rip), %rax` would be the AT&T equivalent and Intel (NASM) syntax would be `lea rax, [REL var]`. I assume though you are using INtel syntax with GNU assembler (GAS)? – Michael Petch Jul 17 '19 at 11:27
  • 1
    @MichaelPetch: Agreed this is GAS `.intel_syntax noprefix`. The syntax for RIP-relative is `[RIP + var]`. – Peter Cordes Jul 17 '19 at 11:31
  • @PeterCordes : My comment suggested this was probably GAS – Michael Petch Jul 17 '19 at 11:32
  • 2
    I was motivating the OP to make it clear in the question what they were using. especially since they aren't entirely new to asking questions. – Michael Petch Jul 17 '19 at 11:35
  • I appreciate the help! I was indeed using GNU assembler. Your solution worked @PeterCordes. Thanks a lot! – heroxav Jul 17 '19 at 12:32

0 Answers0