Search code examples
cldlinker-scripts

Customize function placing in linker script


Motivation

I'm learning how to use and write my own linker scripts.

The problem

Currently, I have 2 simple functions in a .c file and I'd like to put each of those functions in separate sections, but I couldn't found out how to "access" those functions in the linker script.

Reproduction steps

Those two files are all in the same directory:

file1.c

int func1() {
    return 10;
}

char * func3() {
    return (void *) 0;
}

linker.ld

SECTIONS {
    .text 0xDEADBEEF : {
        file1.o (.text.func1)
    }

    .data 0x4 : {
        file1.o (.text.func3)
    }
}

Now do the following:

gcc -c file1.c
ld -T linker.ld file1.o

Expected/Desired output

What I expect to get from objdump -d a.out:


a.out:     file format elf64-x86-64


Disassembly of section .text:

00000000deadbeef <func1>:
    deadbeef:   55                      push   %rbp
    deadbef0:   48 89 e5                mov    %rsp,%rbp
    deadbef3:   b8 0a 00 00 00          mov    $0xa,%eax
    deadbef8:   5d                      pop    %rbp
    deadbef9:   c3                      ret    

Disassembly of section .data:

0000000000000004 <func3>:
    deadbefa:   55                      push   %rbp
    deadbefb:   48 89 e5                mov    %rsp,%rbp
    deadbefe:   b8 00 00 00 00          mov    $0x0,%eax
    deadbf03:   5d                      pop    %rbp
    deadbf04:   c3                      ret    

Or if it's possible I'd like to have those functions in custom-named sections like customSection1 and customSection2.

What I got

I got the following from objdump -d a.out:


a.out:     file format elf64-x86-64


Disassembly of section .text:

00000000deadbeef <func1>:
    deadbeef:   55                      push   %rbp
    deadbef0:   48 89 e5                mov    %rsp,%rbp
    deadbef3:   b8 0a 00 00 00          mov    $0xa,%eax
    deadbef8:   5d                      pop    %rbp
    deadbef9:   c3                      ret    

00000000deadbefa <func3>:
    deadbefa:   55                      push   %rbp
    deadbefb:   48 89 e5                mov    %rsp,%rbp
    deadbefe:   b8 00 00 00 00          mov    $0x0,%eax
    deadbf03:   5d                      pop    %rbp
    deadbf04:   c3                      ret    

Other information

  • I'm aware that the names in the brackets of <filename> (<here>) should be sections, not function names. However, I thought that <section>.<function> could work, which does not work, as you can see
  • I don't know if it's uncommon to put code into the .data section. I just read in the ld manual that a.out supports only the section names, .text, .bss and .data:

In formats which only support a limited number of sections, such as a.out, the name must be one of the names supported by the format (a.out, for example, allows only .text, .data or .bss).

So I chose .data.


Solution

  • In order to instruct the compiler to place each function in a separate section (named as you would expect .text.<function_name> or similar) you need compile with the -ffunction-sections option (for GCC).
    See the documentation here