1

In C, we can force the linker to put a specific function in a specific section from the source code, using something like the following example.

Here, the function my_function is tagged with the preprocessor macro PUT_IN_USER_SECTION in order to tell the linker to put it in the section .user_section.

#define PUT_IN_USER_SECTION  __attribute__((__section__(".user_section"))) __attribute__ ((noinline))

double PUT_IN_USER_SECTION my_function(double a, double b)
{
    // Function content
}

Now, what I'd like to know is, when we use standard functions (for example log functions from the math.h library) from the GLIBC, MUSL, ... and we perform static linking: is it possible to put those functions in specific sections? and how to that?

noureddine-as
  • 453
  • 4
  • 12
  • Why not just `objcopy`? – KamilCuk Dec 10 '20 at 10:05
  • @KamilCuk I thought about that, however, by default (using gcc + musl + static linking) the math functions are included in the `.text` section. How can I isolate only the math functions for example? – noureddine-as Dec 10 '20 at 11:00
  • 1
    Well, you could list all math function (along `nm libm.a`) and compile with `--function-sections` then move all `.text.math_function_name` to other section with `objcopy`. But why not use a linker script? Something along `section { *libm.a:(.text) }` in linker script, I guess. – KamilCuk Dec 10 '20 at 11:04

1 Answers1

0

There is a technique called overlaying that comes from a time when memory resources were limited. It is still used in Embedded Systems with restricted resources :

https://en.wikipedia.org/wiki/Overlay_(programming) overlaying

source : https://de.wikipedia.org/wiki/Overlay_(Programmierung)#/media/Datei:Overlay_Programming.svg

In https://developer.arm.com/documentation/100066/0611/mapping-code-and-data-to-target-memory/manual-overlay-support/writing-an-overlay-manager-for-manually-placed-overlays is an implementation of overlaying, the load addresses are in the scatter file.

https://sourceware.org/gdb/onlinedocs/gdb/How-Overlays-Work.html

For the addresses of functions in memory see https://ftp.gnu.org/pub/old-gnu/Manuals/ld-2.9.1/html_node/ld_22.html

https://www.keil.com/support/man/docs/armlink/armlink_pge1362066004071.htm

ralf htp
  • 9,149
  • 4
  • 22
  • 34