4

I have a precompiled library compiled with a gcc-based compiler and I would like to move the functions from the default .text section to some other section name (let's say foo). Is there a way to do this using binutils without recompiling?

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • Edit the linker script. – too honest for this site Mar 15 '17 at 19:19
  • ? That has no bearing on this question; the linker doesn't affect this library at all, it's just the compiler and the `ar` utility. – Jason S Mar 15 '17 at 19:23
  • TRhe linker is responsible to link the final program code. Ant that's where you can mpa the sections from each input file to the output's sections resp. segments. So yes, it very well is involved. You are just looking at the wrong place. What do you expect will happen with an unknow section from an input file when you eventually link it? – too honest for this site Mar 15 '17 at 19:32
  • 2
    You can use [`objcopy --rename-section`](http://man7.org/linux/man-pages/man1/objcopy.1.html), for example. – Nominal Animal Mar 15 '17 at 19:34
  • @Olaf "What do you expect will happen with an unknow section from an input file when you eventually link it?" -- our linker script places sections that don't otherwise match, into the `.text` output section, so it doesn't change how things are linked. I'm just trying to partition program object code from this particular library into a contiguous area. – Jason S Mar 15 '17 at 20:20
  • That sounds like a bad idea. You normally discard such sections not to clutter the binary with unexpected stuff. What do you mean with "continous area"? The same section of a module is normally not split by the linker. – too honest for this site Mar 15 '17 at 20:51
  • @NominalAnimal -- thanks! Post as an answer and I'll accept. This worked great for me and I was able to see more clearly how much program and data memory was used by a particular library. – Jason S Mar 15 '17 at 21:14

2 Answers2

3

.a library or static library is nothing but collection of object files only.

So before linking you can use objcpy rename command to change name of your sections. Similarly you can change symbol name also using " -redefine-sym"

--rename-section oldname=newname[,flags] Rename a section from oldname to newname, optionally changing the section's flags to flags in the process. This has the advantage over using a linker script to perform the rename in that the output stays as an object file and does not become a linked executable.

Usage:

objcopy -I binary -O <output_format> -B <architecture>
    --rename-section .data=.rodata,alloc,load,readonly,data,contents
    <input_binary_file> <output_object_file>

Refer: http://man7.org/linux/man-pages/man1/objcopy.1.html

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
Varun Sharma
  • 530
  • 5
  • 10
1

Linker Script

You can also us ld to rename sections using a linker script.

ld -r -T section_rename.xsc -o output.o input.o

Where the section_rename.xsc might look similar to the following:

SECTIONS
{
  .newtext : { *(.text) }
  .newdata : { *(.data) }
}
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126