llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

llvm-size reports non-write sections under "data"

Open llvmbot opened this issue 8 years ago • 3 comments

Bugzilla Link 38723
Resolution FIXED
Resolved on Jan 03, 2019 14:46
Version trunk
OS Linux
Attachments Binary that demonstrates the problem
Reporter LLVM Bugzilla Contributor
CC @rupprecht
Fixed by commit(s) https://reviews.llvm.org/rL349074

Extended Description

Compare these two outputs

LLVM - https://github.com/llvm-mirror/llvm/commit/24535f7c19077d02b193bc6225a667f963901eb4

$ llvm-size app
   text    data     bss     dec     hex filename
    160    1052       4    1216     4c0 app

GNU objdump (GNU Binutils) 2.31.1

$ size app
   text    data     bss     dec     hex filename
   1208       4       4    1216     4c0 app

Now look at the section headers of the app binary (attached)

$ readelf -S app
There are 20 section headers, starting at offset 0x2652c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .vector_table     PROGBITS        08000000 010000 000400 00   A  0   0  4
  [ 2] .text             PROGBITS        08000400 010400 0000a0 00  AX  0   0  2
  [ 3] .rodata           PROGBITS        080004a0 0104a0 000018 00   A  0   0  4
  [ 4] .data             PROGBITS        20000000 020000 000004 00  WA  0   0  4
  [ 5] .bss              NOBITS          20000004 020004 000004 00  WA  0   0  4
  [ 6] .debug_str        PROGBITS        00000000 020004 00114f 01  MS  0   0  1
  [ 7] .debug_loc        PROGBITS        00000000 021153 000148 00      0   0  1
  [ 8] .debug_abbrev     PROGBITS        00000000 02129b 00036c 00      0   0  1
  [ 9] .debug_info       PROGBITS        00000000 021607 001ab0 00      0   0  1
  [10] .debug_ranges     PROGBITS        00000000 0230b7 000048 00      0   0  1
  [11] .debug_macinfo    PROGBITS        00000000 0230ff 000001 00      0   0  1
  [12] .debug_pubnames   PROGBITS        00000000 023100 000a20 00      0   0  1
  [13] .debug_pubtypes   PROGBITS        00000000 023b20 00096f 00      0   0  1
  [14] .ARM.attributes   ARM_ATTRIBUTES  00000000 02448f 00002e 00      0   0  1
  [15] .debug_frame      PROGBITS        00000000 0244c0 000084 00      0   0  4
  [16] .debug_line       PROGBITS        00000000 024544 0005cd 00      0   0  1
  [17] .symtab           SYMTAB          00000000 024b14 0017c0 10     18 351  4
  [18] .strtab           STRTAB          00000000 0262d4 000180 00      0   0  1
  [19] .shstrtab         STRTAB          00000000 026454 0000d8 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  y (purecode), p (processor specific)

llvm-size reports .vector_table, .rodata and .data under "data" (1052 bytes) whereas GNU size only reports .data under "data" (4 bytes).

I would expect .vector_table and .rodata, which don't have the write (W) flag set, to be reported under "text", not "data" -- which what the GNU tool does

llvmbot avatar Aug 27 '18 15:08 llvmbot

I'm able to get size and llvm-size to agree by making the following changes in libobject (for ELF files):

  1. isSectionText needs to include sections where SHF_WRITE is not set
  2. isSectionText needs to only consider sections which have SHF_ALLOC
  3. isSectionData needs to check != SHT_NOBITS instead of checking == SHT_PROGBITS, e.g. to allow things like SHT_INIT_ARRAY.

When I change it, a handful of tests (~15) break, but they don't look too hard to patch up.

However, I'm not sure if all of those three fixes are the right way to go. I can't seem to find any spec that says what size is supposed to produce -- POSIX explicitly omits it, and GNU man pages just says it "lists the section sizes" without describing how it breaks down text/data/bss. (2) and (3) seem very reasonable but I'm not so sure about (1); personally, when I think "text" I think "code that executes" and therefore only SHF_EXECINSTR. And when I think "data" I think "bytes that are read/written by other instructions; never executed on their own".

I guess whatever we decide with this bug, we should at least document specifically what we're computing.

rupprecht avatar Nov 08 '18 22:11 rupprecht

I happen to be looking at the same thing -- I might as well accept this bug :)

Here's LLVM's snippet:

  // isText():
  //   return sh_flags & ELF::SHF_EXECINSTR
  bool isText = Section.isText();

  // isData():
  //   return sh_type == ELF::SHT_PROGBITS &&
  //       sh_flags & ELF::SHF_ALLOC &&
  //       !(sh_flags & ELF::SHF_EXECINSTR);
  bool isData = Section.isData();

  // isBSS():
  //   return sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
  //       sh_type == ELF::SHT_NOBITS;
  bool isBSS = Section.isBSS();

  if (isText)
    total_text += size;
  else if (isData)
    total_data += size;
  else if (isBSS)
    total_bss += size;

Compared to GNU size:

  // Basically: SEC_CODE ~= ELF::SHF_EXECINSTR, SEC_READONLY ~= ~ELF::SHF_WRITE
  if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
    textsize += size;
  // Not sure what exactly SEC_HAS_CONTENTS maps to...
  else if ((flags & SEC_HAS_CONTENTS) != 0)
    datasize += size;
  else
    bsssize += size;

Note that a side effect of this is that even totals don't always agree (although it does for your case) -- it's possible for something to fall LLVM's if/else and not be interpreted as any of text, data, or bss.

rupprecht avatar Nov 08 '18 01:11 rupprecht

assigned to @rupprecht

llvmbot avatar Aug 27 '18 15:08 llvmbot