GNU binutils (ld linker, objdump, readelf, strip)

GNU Binary Utilities

Dependencies

GNU Binutils
Binary File Descriptor library (BFD)
Instruction Set Architecture (ISA)

Object File Format (executable files, object code, shared libraries, and core dumps)

Deutsch

a.out
ELF
COFF
Fat Binary
Universal Binary

English

An object file is a file containing object code, meaning relocatable format machine code that is usually not directly executable. In addition to the object code itself, object files may contain metadata used for linking or debugging, including: information to resolve symbolic cross-references between different modules, relocation information, stack unwinding information, comments, program symbols, debugging or profiling information.

Executable
Object file
a.out
ELF
COFF
Mach-O

Debugging formats

GNU Debugger
Debugging formats DWARF and STAB
DWARF
stabs

Target Selection

Target Selection

$ objdump.exe -i
$ objdump.exe -H
supported targets (object file format):
pe-x86-64
pei-x86-64
pe-bigobj-x86-64
elf64-x86-64
elf64-l1om
elf64-k1om
pe-i386
pei-i386
elf32-i386
elf32-iamcu
elf64-little
elf64-big
elf32-little
elf32-big
plugin
srec
symbolsrec
verilog
tekhex
binary
ihex

supported architectures:
i386
i386:x86-64
i386:x64-32
i8086
i386:intel
i386:x86-64:intel
i386:x64-32:intel
i386:nacl
i386:x86-64:nacl
i386:x64-32:nacl
iamcu
iamcu:intel
l1om
l1om:intel
k1om
k1om:intel
plugin


$ arm-none-eabi-objdump.exe -i
$ arm-none-eabi-objdump.exe -H
supported targets (object file format):
elf32-littlearm
elf32-bigarm
elf32-little
elf32-big
plugin
srec
symbolsrec
verilog
tekhex
binary
ihex

supported architectures:
arm
armv2
armv2a
armv3
armv3m
armv4
armv4t
armv5
armv5t
armv5te
xscale
ep9312
iwmmxt
iwmmxt2
arm_any
plugin

ld

Option Purpose
-b input-format
--format=input-format
What input format is used. You may want to use this option if you are linking files with an unusual binary format. You can also use -b to switch formats explicitly. You can list the available binary formats with objdump -i
-r
--relocatable
Generate relocatable output—i.e., generate an output file that can in turn serve as input to ld. This is often called partial linking.
This option does the same thing as -i.
-i Perform an incremental link (same as option -r).
-o output
--output=output
Use output as the name for the program produced by ld; if this option is not specified, the name a.out is used by default.
-T scriptfile
--script=scriptfile
Use scriptfile as the linker script. This script replaces ld‘s default linker script (rather than adding to it), so commandfile must specify everything necessary to describe the output file. If scriptfile does not exist in the current directory, ld looks for it in the directories specified by any preceding -L options. Multiple -T options accumulate.

Linker Script

LD
LD: Linker Scripts
Linker Scripts
Linker Scripts

Red Hat Enterprise Linux 4: Using ld, the Gnu Linker – Linker Scripts

Chapter 4. Linker Scripts
4.2. Linker Script Format
4.3. Simple Linker Script Example
4.4. Simple Linker Script Commands
4.5. Assigning Values to Symbols
4.6. SECTIONS Command
4.7. MEMORY Command
4.8. PHDRS Command
4.9. VERSION Command
4.10. Expressions in Linker Scripts
4.11. Implicit Linker Scripts

objdump – displays information about one or more object files

man objdump

Linux Objdump Command Examples (Disassemble a Binary File)

objcopy – copies the contents of an object file to another

man objcopy

Option Purpose
-I bfdname
--input-target=bfdname
Consider the source file’s object format to be bfdname, rather than attempting to deduce (herleiten/r├╝ckschliessen) it.
-O bfdname
--output-target=bfdname
Write the output file using the object format bfdname.
-B bfdarch
--binary-architecture=bfdarch
Useful when transforming a architecture-less input file into an object file. In this case the output architecture can be set to bfdarch.
--rename-section oldname=newname[,flags] Rename a section from oldname to newname, optionally changing the section’s flags to flags in the process.
This option is particularly helpful when the input format is binary, since this will always create a section called .data. If for example, you wanted instead to create a section called .rodata containing binary data you could use a command line to achieve it.
objcopy              \
-I binary            \
-O <output_format>   \
-B <architecture>    \
--rename-section .data=.rodata,alloc,load,readonly,data,contents \
<input_binary_file>  \
<output_object_file>

nm – list symbols from object file

man nm

Example Scripts

bin2elf.sh

#!/bin/sh
# Convert a raw binary image into an ELF file suitable for loading into a disassembler

cat > raw$$.ld <<EOF
SECTIONS
{
EOF

echo " . = $3;" >> raw$$.ld

cat >> raw$$.ld <<EOF
  .text : { *(.text) }
}
EOF

CROSS_PREFIX=arm-none-eabi-

${CROSS_PREFIX}ld -b binary -r -o raw$$.elf $1
${CROSS_PREFIX}objcopy  --rename-section .data=.text \
                        --set-section-flags .data=alloc,code,load raw$$.elf
${CROSS_PREFIX}ld raw$$.elf -T raw$$.ld -o $2
${CROSS_PREFIX}strip -s $2

rm -rf raw$$.elf raw$$.ld

Leave a Reply

Your email address will not be published. Required fields are marked *