0

I assembled a simple "Hello, world" program and linked it using TCC, after which I got 4196 bytes of an executable.

The program has 31 sections: ['', '.text', '.data', '.bss', '.symtab', '.strtab', '.rel.text', '.rodata', '.rodata.cst4', '.note.GNU-stack', '.init', '.rel.init', '.gnu.linkonce.t.__x86.get_pc_thunk.bx', '.fini', '.rel.fini', '.text.unlikely', '.text.__x86.get_pc_thunk.bx', '.eh_frame', '.rel.eh_frame', '.preinit_array', '.init_array', '.fini_array', '.interp', '.dynsym', '.dynstr', '.hash', '.dynamic', '.got', '.plt', '.rel.got', '.shstrtab']. I feel that's a real lot for such a simple binary - which ones are actually necessary here for my program to run?

Here's the source code and the way I compiled it:

        extern printf
        global main

        section .data
msg:    db "Hello World!", 0

        section .text
main:
        ;;  puts (msg)
        push msg
        call printf
        add esp, 4

        ;;  return 0
        mov eax, 0
        ret

nasm main.asm -f elf32 && tcc main.o -o main

Tested on 32bit/ubuntu:16.04 Docker image.

Note: this question is different from this one in that I'm not looking for a tensy Linux ELF, but one that allows me to call dynamic symbols. I believe that due to the nature of dynamic linking, I need some extra sections.

d33tah
  • 10,999
  • 13
  • 68
  • 158
  • 1
    Show us the source code and the output of *objdump* (or similar), not a base64 encoded binary. Are you using the CRT? Debug symbols? – Margaret Bloom Dec 25 '16 at 13:12
  • @MargaretBloom: edited the question. – d33tah Dec 25 '16 at 13:15
  • 1
    Possible duplicate of [What sections are required for a usable ELF executable?](http://stackoverflow.com/questions/3832801/what-sections-are-required-for-a-usable-elf-executable) – Michael Foukarakis Dec 25 '16 at 13:20
  • @MichaelFoukarakis: not really. I mean a dynamically-linked ELF, which is another story. – d33tah Dec 25 '16 at 13:25
  • 1
    An ELF is an ELF is an ELF. How does your question differ? Be specific. – Michael Foukarakis Dec 25 '16 at 13:27
  • @MichaelFoukarakis: I want an ELF that is able to call `printf` from libc. This adds extra constraints because of the nature of dynamic linking. I suspect that at the very least I also need `.interp`, but I don't know what else. – d33tah Dec 25 '16 at 13:29
  • @d33tah Will [strip](http://www.thegeekstuff.com/2012/09/strip-command-examples/) help you? – Margaret Bloom Dec 25 '16 at 13:35
  • Possible duplicate of [What are good practices regarding shared libraries on Linux?](http://stackoverflow.com/questions/4757121/what-are-good-practices-regarding-shared-libraries-on-linux) Pay particular attention to the answer that cite's Ulrich Drepper's *"How to write shared libraries"*. – jww Dec 25 '16 at 13:40
  • @d33tah, that (too) is addressed in the linked question. – Michael Foukarakis Dec 25 '16 at 13:44
  • 4
    Strictly speaking I don't think you need any sections in a ELF executable. The dynamic loader only uses the information in the program header and what it refers to load and dynamically link the executable. Much of that information does correspond to certain named sections, but what you actually need is a program header with properly filled out LOAD, INTERP and DYNAMIC segments. – Ross Ridge Dec 25 '16 at 18:38
  • So, after all the edits and clarifications, is this a duplicate of either of the suggested dups? It looks like maybe not, so I'll hold off on dup-hammering this. – Peter Cordes Dec 26 '16 at 02:12

1 Answers1

2

I believe that due to the nature of dynamic linking, I need some extra sections.

Your belief is mistaken. No section is necessary at runtime, only segments matter.

A runnable dynamically-linked ELF binary will have at least one PT_LOAD segment, a PT_INTERP segment, and PT_DYNAMIC segment.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362