[LLVMdev] Detrimental optimization for reducing relocations.

Jan Sjodin
Thu, 10 Mar 2011 21:06:27 -0000

I was looking into the AsmPrinter and the method EmitSectionOffset which
contains this code:

--------------------------------------------------------------------------------
  // If the section in question will end up with an address of 0 anyway, we can
  // just emit an absolute reference to save a relocation.
  if (Section.isBaseAddressKnownZero()) {
    OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
    return;
  }

  // Otherwise, emit it as a label difference from the start of the section.
  EmitLabelDifference(Label, SectionLabel, 4);
}
--------------------------------------------------------------------------------
isBaseAddrfessKnownZero() only returns true for some MCSectionELF sections
(always false for MachO and COFF), however emitting a symbol value seems to
always cause a relocation entry, but a label difference does not. I compiled the
factorial program from the demo page with debug info and dumped the relocation
entries, then I commented out the top block so that EmitLabelDifference was
always called.

Original:
objdump -r directsymbol.o
directsymbol.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000005a R_X86_64_PC32     atoi-0x0000000000000004
0000000000000066 R_X86_64_32       .rodata.str1.1
000000000000006f R_X86_64_PC32     printf-0x0000000000000004


RELOCATION RECORDS FOR [.debug_frame]:
OFFSET           TYPE              VALUE
0000000000000018 R_X86_64_32       .debug_frame
000000000000001c R_X86_64_64       .text
0000000000000040 R_X86_64_32       .debug_frame
0000000000000044 R_X86_64_64       .text+0x0000000000000040


RELOCATION RECORDS FOR [.debug_info]:
OFFSET           TYPE              VALUE
0000000000000006 R_X86_64_32       .debug_abbrev
0000000000000097 R_X86_64_64       .text
000000000000009f R_X86_64_64       .text+0x0000000000000034
00000000000000c9 R_X86_64_64       .text+0x0000000000000040
00000000000000d1 R_X86_64_64       .text+0x000000000000007c


RELOCATION RECORDS FOR [.debug_line]:
OFFSET           TYPE              VALUE
000000000000002f R_X86_64_64       .text


RELOCATION RECORDS FOR [.debug_pubnames]:
OFFSET           TYPE              VALUE
0000000000000006 R_X86_64_32       .debug_info


RELOCATION RECORDS FOR [.debug_pubtypes]:
OFFSET           TYPE              VALUE
0000000000000006 R_X86_64_32       .debug_info

Then with the reduced code, without the optimization:
objdump -r labeldiff.o

labeldiff.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000005a R_X86_64_PC32     atoi-0x0000000000000004
0000000000000066 R_X86_64_32       .rodata.str1.1
000000000000006f R_X86_64_PC32     printf-0x0000000000000004


RELOCATION RECORDS FOR [.debug_frame]:
OFFSET           TYPE              VALUE
000000000000001c R_X86_64_64       .text
0000000000000044 R_X86_64_64       .text+0x0000000000000040


RELOCATION RECORDS FOR [.debug_info]:
OFFSET           TYPE              VALUE
0000000000000097 R_X86_64_64       .text
000000000000009f R_X86_64_64       .text+0x0000000000000034
00000000000000c9 R_X86_64_64       .text+0x0000000000000040
00000000000000d1 R_X86_64_64       .text+0x000000000000007c


RELOCATION RECORDS FOR [.debug_line]:
OFFSET           TYPE              VALUE
000000000000002f R_X86_64_64       .text

So, clearly the optimization is making things worse. Would it be okay to delete
this code and eliminate the isBaseAddressKnownZero? I would like to get rid of
it.

- Jan