{"id":3012,"date":"2014-09-22T07:44:08","date_gmt":"2014-09-22T07:44:08","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=3012"},"modified":"2017-07-13T09:06:17","modified_gmt":"2017-07-13T09:06:17","slug":"gnu-ld-linker","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=3012","title":{"rendered":"GNU binutils (ld linker, objdump, readelf, strip)"},"content":{"rendered":"<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/binutils\/index.html\">GNU Binary Utilities<\/a><\/p>\n<h3>Dependencies<\/h3>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/GNU_Binutils\">GNU Binutils<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Binary_File_Descriptor_library\">Binary File Descriptor library (BFD)<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Instruction_set_architecture\">Instruction Set Architecture (ISA)<\/a><\/p>\n<h3>Object File Format (executable files, object code, shared libraries, and core dumps)<\/h3>\n<h4>Deutsch<\/h4>\n<p><a href=\"https:\/\/de.wikipedia.org\/wiki\/A.out\">a.out<\/a><br \/>\n<a href=\"https:\/\/de.wikipedia.org\/wiki\/Executable_and_Linking_Format\">ELF<\/a><br \/>\n<a href=\"https:\/\/de.wikipedia.org\/wiki\/Common_Object_File_Format\">COFF<\/a><br \/>\n<a href=\"https:\/\/de.wikipedia.org\/wiki\/Fat_Binary\">Fat Binary<\/a><br \/>\n<a href=\"https:\/\/de.wikipedia.org\/wiki\/Universal_Binary\">Universal Binary<\/a><\/p>\n<h4>English<\/h4>\n<blockquote><p>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.<\/p><\/blockquote>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Executable\">Executable<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object_file\">Object file<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/A.out\">a.out<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Executable_and_Linkable_Format\">ELF<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/COFF\">COFF<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Mach-O\">Mach-O<\/a><\/p>\n<h3>Debugging formats<\/h3>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/GNU_Debugger\">GNU Debugger<\/a><br \/>\n<a href=\"https:\/\/www.ibm.com\/developerworks\/library\/os-debugging\/\">Debugging formats DWARF and STAB<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/DWARF\">DWARF<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Stabs\">stabs<\/a><\/p>\n<h3>Target Selection<\/h3>\n<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/binutils\/Target-Selection.html#Target-Selection\">Target Selection<\/a><\/p>\n<pre class=\"brush: plain; collapse: true; light: false; title: List of supported targets and architectures; toolbar: true; notranslate\" title=\"List of supported targets and architectures\">\r\n$ objdump.exe -i\r\n$ objdump.exe -H\r\nsupported targets (object file format):\r\npe-x86-64\r\npei-x86-64\r\npe-bigobj-x86-64\r\nelf64-x86-64\r\nelf64-l1om\r\nelf64-k1om\r\npe-i386\r\npei-i386\r\nelf32-i386\r\nelf32-iamcu\r\nelf64-little\r\nelf64-big\r\nelf32-little\r\nelf32-big\r\nplugin\r\nsrec\r\nsymbolsrec\r\nverilog\r\ntekhex\r\nbinary\r\nihex\r\n\r\nsupported architectures:\r\ni386\r\ni386:x86-64\r\ni386:x64-32\r\ni8086\r\ni386:intel\r\ni386:x86-64:intel\r\ni386:x64-32:intel\r\ni386:nacl\r\ni386:x86-64:nacl\r\ni386:x64-32:nacl\r\niamcu\r\niamcu:intel\r\nl1om\r\nl1om:intel\r\nk1om\r\nk1om:intel\r\nplugin\r\n\r\n\r\n$ arm-none-eabi-objdump.exe -i\r\n$ arm-none-eabi-objdump.exe -H\r\nsupported targets (object file format):\r\nelf32-littlearm\r\nelf32-bigarm\r\nelf32-little\r\nelf32-big\r\nplugin\r\nsrec\r\nsymbolsrec\r\nverilog\r\ntekhex\r\nbinary\r\nihex\r\n\r\nsupported architectures:\r\narm\r\narmv2\r\narmv2a\r\narmv3\r\narmv3m\r\narmv4\r\narmv4t\r\narmv5\r\narmv5t\r\narmv5te\r\nxscale\r\nep9312\r\niwmmxt\r\niwmmxt2\r\narm_any\r\nplugin\r\n<\/pre>\n<h3>ld<\/h3>\n<table>\n<tr>\n<td nowrap>Option<\/td>\n<td>Purpose<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-b input-format<br \/>--format=input-format<\/code><\/td>\n<td>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 <code>-b<\/code> to switch formats explicitly. You can list the available binary formats with <code>objdump -i<\/code><\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-r<br \/>--relocatable<\/code><\/td>\n<td>Generate relocatable output&#8212;i.e., generate an output file that can in turn serve as input to <code>ld<\/code>. This is often called partial linking.<br \/>This option does the same thing as -i.<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-i<\/code><\/td>\n<td>Perform an incremental link (same as option <code>-r<\/code>).<\/code><\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-o output<br \/>--output=output<\/code><\/td>\n<td>Use output as the name for the program produced by <code>ld<\/code>; if this option is not specified, the name a.out is used by default.<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-T scriptfile<br \/>--script=scriptfile<\/code><\/td>\n<td>Use scriptfile as the linker script. This script replaces <code>ld<\/code>&#8216;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, <code>ld<\/code> looks for it in the directories specified by any preceding <code>-L<\/code> options. Multiple <code>-T<\/code> options accumulate.<\/td>\n<\/tr>\n<\/table>\n<h4>Linker Script<\/h4>\n<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/ld\/\">LD<\/a><br \/>\n<a href=\"https:\/\/sourceware.org\/binutils\/docs\/ld\/Scripts.html\">LD: Linker Scripts<\/a><br \/>\n<a href=\"http:\/\/www.math.utah.edu\/docs\/info\/ld_3.html\">Linker Scripts<\/a><br \/>\n<a href=\"http:\/\/www.scoberlin.de\/content\/media\/http\/informatik\/gcc_docs\/ld_3.html\">Linker Scripts<\/a><\/p>\n<h4>Red Hat Enterprise Linux 4: Using ld, the Gnu Linker &#8211; Linker Scripts<\/h4>\n<p><a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/scripts.html\">Chapter 4. Linker Scripts<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/script-format.html\">4.2. Linker Script Format<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/simple-example.html\">4.3. Simple Linker Script Example<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/simple-commands.html\">4.4. Simple Linker Script Commands<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/assignments.html\">4.5. Assigning Values to Symbols<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/sections.html\">4.6. SECTIONS Command<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/memory.html\">4.7. MEMORY Command<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/phdrs.html\">4.8. PHDRS Command<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/version.html\">4.9. VERSION Command<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/expressions.html\">4.10. Expressions in Linker Scripts<\/a><br \/>\n<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/4\/html\/Using_ld_the_GNU_Linker\/implicit-linker-scripts.html\">4.11. Implicit Linker Scripts<\/a><\/p>\n<h3>objdump &#8211; displays information about one or more object files<\/h3>\n<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/binutils\/objdump.html\">man objdump<\/a><\/p>\n<p><a href=\"http:\/\/www.thegeekstuff.com\/2012\/09\/objdump-examples\/?utm_source=feedburner\">Linux Objdump Command Examples (Disassemble a Binary File)<\/a><\/p>\n<h3>objcopy &#8211; copies the contents of an object file to another<\/h3>\n<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/binutils\/objcopy.html\">man objcopy<\/a><\/p>\n<table>\n<tr>\n<td nowrap>Option<\/td>\n<td>Purpose<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-I bfdname<br \/>--input-target=bfdname<\/code><\/td>\n<td>Consider the source file&#8217;s object format to be <code>bfdname<\/code>, rather than attempting to deduce (herleiten\/r\u00fcckschliessen) it.<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-O bfdname<br \/>--output-target=bfdname<\/code><\/td>\n<td>Write the output file using the object format <code>bfdname<\/code>.<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>-B bfdarch<br \/>--binary-architecture=bfdarch<\/code><\/td>\n<td>Useful when transforming a architecture-less input file into an object file. In this case the output architecture can be set to <code>bfdarch<\/code>.<\/td>\n<\/tr>\n<tr>\n<td nowrap><code>--rename-section oldname=newname[,flags]<\/code><\/td>\n<td>Rename a section from oldname to newname, optionally changing the section&#8217;s flags to flags in the process.<br \/>This option is particularly helpful when the input format is binary, since this will always create a section called <code>.data<\/code>. If for example, you wanted instead to create a section called <code>.rodata<\/code> containing binary data you could use a command line to achieve it.<\/td>\n<\/tr>\n<\/table>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nobjcopy              \\\r\n-I binary            \\\r\n-O &lt;output_format&gt;   \\\r\n-B &lt;architecture&gt;    \\\r\n--rename-section .data=.rodata,alloc,load,readonly,data,contents \\\r\n&lt;input_binary_file&gt;  \\\r\n&lt;output_object_file&gt;\r\n<\/pre>\n<h3>nm &#8211; list symbols from object file<\/h3>\n<p><a href=\"https:\/\/sourceware.org\/binutils\/docs\/binutils\/nm.html\">man nm<\/a><\/p>\n<h3>Example Scripts<\/h3>\n<p><a href=\"https:\/\/gist.github.com\/tangrs\/4030336\"><code>bin2elf.sh<\/code><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/sh\r\n# Convert a raw binary image into an ELF file suitable for loading into a disassembler\r\n\r\ncat &gt; raw$$.ld &lt;&lt;EOF\r\nSECTIONS\r\n{\r\nEOF\r\n\r\necho &quot; . = $3;&quot; &gt;&gt; raw$$.ld\r\n\r\ncat &gt;&gt; raw$$.ld &lt;&lt;EOF\r\n  .text : { *(.text) }\r\n}\r\nEOF\r\n\r\nCROSS_PREFIX=arm-none-eabi-\r\n\r\n${CROSS_PREFIX}ld -b binary -r -o raw$$.elf $1\r\n${CROSS_PREFIX}objcopy  --rename-section .data=.text \\\r\n                        --set-section-flags .data=alloc,code,load raw$$.elf\r\n${CROSS_PREFIX}ld raw$$.elf -T raw$$.ld -o $2\r\n${CROSS_PREFIX}strip -s $2\r\n\r\nrm -rf raw$$.elf raw$$.ld\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3012","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3012"}],"version-history":[{"count":19,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions"}],"predecessor-version":[{"id":6488,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions\/6488"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}