ldwrite.c revision 38889
133965Sjdp/* ldwrite.c -- write out the linked file 238889Sjdp Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998 338889Sjdp Free Software Foundation, Inc. 433965Sjdp Written by Steve Chamberlain sac@cygnus.com 533965Sjdp 633965SjdpThis file is part of GLD, the Gnu Linker. 733965Sjdp 833965SjdpThis program is free software; you can redistribute it and/or modify 933965Sjdpit under the terms of the GNU General Public License as published by 1033965Sjdpthe Free Software Foundation; either version 2 of the License, or 1133965Sjdp(at your option) any later version. 1233965Sjdp 1333965SjdpThis program is distributed in the hope that it will be useful, 1433965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of 1533965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1633965SjdpGNU General Public License for more details. 1733965Sjdp 1833965SjdpYou should have received a copy of the GNU General Public License 1933965Sjdpalong with this program; if not, write to the Free Software 2033965SjdpFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 2133965Sjdp 2233965Sjdp#include "bfd.h" 2333965Sjdp#include "sysdep.h" 2433965Sjdp#include "bfdlink.h" 2533965Sjdp#include "libiberty.h" 2633965Sjdp 2733965Sjdp#include "ld.h" 2833965Sjdp#include "ldexp.h" 2933965Sjdp#include "ldlang.h" 3033965Sjdp#include "ldwrite.h" 3133965Sjdp#include "ldmisc.h" 3233965Sjdp#include "ldgram.h" 3333965Sjdp#include "ldmain.h" 3433965Sjdp 3533965Sjdpstatic void build_link_order PARAMS ((lang_statement_union_type *)); 3633965Sjdpstatic asection *clone_section PARAMS ((bfd *, asection *, int *)); 3733965Sjdpstatic void split_sections PARAMS ((bfd *, struct bfd_link_info *)); 3833965Sjdp 3933965Sjdp/* Build link_order structures for the BFD linker. */ 4033965Sjdp 4133965Sjdpstatic void 4233965Sjdpbuild_link_order (statement) 4333965Sjdp lang_statement_union_type *statement; 4433965Sjdp{ 4533965Sjdp switch (statement->header.type) 4633965Sjdp { 4733965Sjdp case lang_data_statement_enum: 4833965Sjdp { 4933965Sjdp asection *output_section; 5033965Sjdp struct bfd_link_order *link_order; 5133965Sjdp bfd_vma value; 5238889Sjdp boolean big_endian = false; 5333965Sjdp 5433965Sjdp output_section = statement->data_statement.output_section; 5533965Sjdp ASSERT (output_section->owner == output_bfd); 5633965Sjdp 5733965Sjdp link_order = bfd_new_link_order (output_bfd, output_section); 5833965Sjdp if (link_order == NULL) 5933965Sjdp einfo ("%P%F: bfd_new_link_order failed\n"); 6033965Sjdp 6133965Sjdp link_order->type = bfd_data_link_order; 6233965Sjdp link_order->offset = statement->data_statement.output_vma; 6333965Sjdp link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE); 6433965Sjdp 6533965Sjdp value = statement->data_statement.value; 6633965Sjdp 6733965Sjdp /* If the endianness of the output BFD is not known, then we 6833965Sjdp base the endianness of the data on the first input file. 6933965Sjdp By convention, the bfd_put routines for an unknown 7033965Sjdp endianness are big endian, so we must swap here if the 7133965Sjdp input file is little endian. */ 7238889Sjdp if (bfd_big_endian (output_bfd)) 7338889Sjdp big_endian = true; 7438889Sjdp else if (bfd_little_endian (output_bfd)) 7538889Sjdp big_endian = false; 7638889Sjdp else 7733965Sjdp { 7833965Sjdp boolean swap; 7933965Sjdp 8033965Sjdp swap = false; 8138889Sjdp if (command_line.endian == ENDIAN_BIG) 8238889Sjdp big_endian = true; 8338889Sjdp else if (command_line.endian == ENDIAN_LITTLE) 8438889Sjdp { 8538889Sjdp big_endian = false; 8638889Sjdp swap = true; 8738889Sjdp } 8833965Sjdp else if (command_line.endian == ENDIAN_UNSET) 8933965Sjdp { 9038889Sjdp big_endian = true; 9138889Sjdp { 9238889Sjdp LANG_FOR_EACH_INPUT_STATEMENT (s) 9338889Sjdp { 9438889Sjdp if (s->the_bfd != NULL) 9538889Sjdp { 9638889Sjdp if (bfd_little_endian (s->the_bfd)) 9738889Sjdp { 9838889Sjdp big_endian = false; 9938889Sjdp swap = true; 10038889Sjdp } 10138889Sjdp break; 10238889Sjdp } 10338889Sjdp } 10438889Sjdp } 10533965Sjdp } 10633965Sjdp 10733965Sjdp if (swap) 10833965Sjdp { 10933965Sjdp bfd_byte buffer[8]; 11033965Sjdp 11133965Sjdp switch (statement->data_statement.type) 11233965Sjdp { 11333965Sjdp case QUAD: 11438889Sjdp case SQUAD: 11538889Sjdp if (sizeof (bfd_vma) >= QUAD_SIZE) 11638889Sjdp { 11738889Sjdp bfd_putl64 (value, buffer); 11838889Sjdp value = bfd_getb64 (buffer); 11938889Sjdp break; 12038889Sjdp } 12138889Sjdp /* Fall through. */ 12233965Sjdp case LONG: 12333965Sjdp bfd_putl32 (value, buffer); 12433965Sjdp value = bfd_getb32 (buffer); 12533965Sjdp break; 12633965Sjdp case SHORT: 12733965Sjdp bfd_putl16 (value, buffer); 12833965Sjdp value = bfd_getb16 (buffer); 12933965Sjdp break; 13033965Sjdp case BYTE: 13133965Sjdp break; 13233965Sjdp default: 13333965Sjdp abort (); 13433965Sjdp } 13533965Sjdp } 13633965Sjdp } 13733965Sjdp 13833965Sjdp ASSERT (output_section->owner == output_bfd); 13933965Sjdp switch (statement->data_statement.type) 14033965Sjdp { 14133965Sjdp case QUAD: 14238889Sjdp case SQUAD: 14338889Sjdp if (sizeof (bfd_vma) >= QUAD_SIZE) 14438889Sjdp bfd_put_64 (output_bfd, value, link_order->u.data.contents); 14538889Sjdp else 14638889Sjdp { 14738889Sjdp bfd_vma high; 14838889Sjdp 14938889Sjdp if (statement->data_statement.type == QUAD) 15038889Sjdp high = 0; 15138889Sjdp else if ((value & 0x80000000) == 0) 15238889Sjdp high = 0; 15338889Sjdp else 15438889Sjdp high = (bfd_vma) -1; 15538889Sjdp bfd_put_32 (output_bfd, high, 15638889Sjdp (link_order->u.data.contents 15738889Sjdp + (big_endian ? 0 : 4))); 15838889Sjdp bfd_put_32 (output_bfd, value, 15938889Sjdp (link_order->u.data.contents 16038889Sjdp + (big_endian ? 4 : 0))); 16138889Sjdp } 16233965Sjdp link_order->size = QUAD_SIZE; 16333965Sjdp break; 16433965Sjdp case LONG: 16533965Sjdp bfd_put_32 (output_bfd, value, link_order->u.data.contents); 16633965Sjdp link_order->size = LONG_SIZE; 16733965Sjdp break; 16833965Sjdp case SHORT: 16933965Sjdp bfd_put_16 (output_bfd, value, link_order->u.data.contents); 17033965Sjdp link_order->size = SHORT_SIZE; 17133965Sjdp break; 17233965Sjdp case BYTE: 17333965Sjdp bfd_put_8 (output_bfd, value, link_order->u.data.contents); 17433965Sjdp link_order->size = BYTE_SIZE; 17533965Sjdp break; 17633965Sjdp default: 17733965Sjdp abort (); 17833965Sjdp } 17933965Sjdp } 18033965Sjdp break; 18133965Sjdp 18233965Sjdp case lang_reloc_statement_enum: 18333965Sjdp { 18433965Sjdp lang_reloc_statement_type *rs; 18533965Sjdp asection *output_section; 18633965Sjdp struct bfd_link_order *link_order; 18733965Sjdp 18833965Sjdp rs = &statement->reloc_statement; 18933965Sjdp 19033965Sjdp output_section = rs->output_section; 19133965Sjdp ASSERT (output_section->owner == output_bfd); 19233965Sjdp 19333965Sjdp link_order = bfd_new_link_order (output_bfd, output_section); 19433965Sjdp if (link_order == NULL) 19533965Sjdp einfo ("%P%F: bfd_new_link_order failed\n"); 19633965Sjdp 19733965Sjdp link_order->offset = rs->output_vma; 19833965Sjdp link_order->size = bfd_get_reloc_size (rs->howto); 19933965Sjdp 20033965Sjdp link_order->u.reloc.p = 20133965Sjdp ((struct bfd_link_order_reloc *) 20233965Sjdp xmalloc (sizeof (struct bfd_link_order_reloc))); 20333965Sjdp 20433965Sjdp link_order->u.reloc.p->reloc = rs->reloc; 20533965Sjdp link_order->u.reloc.p->addend = rs->addend_value; 20633965Sjdp 20733965Sjdp if (rs->name == NULL) 20833965Sjdp { 20933965Sjdp link_order->type = bfd_section_reloc_link_order; 21033965Sjdp if (rs->section->owner == output_bfd) 21133965Sjdp link_order->u.reloc.p->u.section = rs->section; 21233965Sjdp else 21333965Sjdp { 21433965Sjdp link_order->u.reloc.p->u.section = rs->section->output_section; 21533965Sjdp link_order->u.reloc.p->addend += rs->section->output_offset; 21633965Sjdp } 21733965Sjdp } 21833965Sjdp else 21933965Sjdp { 22033965Sjdp link_order->type = bfd_symbol_reloc_link_order; 22133965Sjdp link_order->u.reloc.p->u.name = rs->name; 22233965Sjdp } 22333965Sjdp } 22433965Sjdp break; 22533965Sjdp 22633965Sjdp case lang_input_section_enum: 22733965Sjdp /* Create a new link_order in the output section with this 22833965Sjdp attached */ 22933965Sjdp if (statement->input_section.ifile->just_syms_flag == false) 23033965Sjdp { 23133965Sjdp asection *i = statement->input_section.section; 23233965Sjdp asection *output_section = i->output_section; 23333965Sjdp 23433965Sjdp ASSERT (output_section->owner == output_bfd); 23533965Sjdp 23633965Sjdp if ((output_section->flags & SEC_HAS_CONTENTS) != 0) 23733965Sjdp { 23833965Sjdp struct bfd_link_order *link_order; 23933965Sjdp 24033965Sjdp link_order = bfd_new_link_order (output_bfd, output_section); 24133965Sjdp 24233965Sjdp if (i->flags & SEC_NEVER_LOAD) 24333965Sjdp { 24433965Sjdp /* We've got a never load section inside one which 24533965Sjdp is going to be output, we'll change it into a 24633965Sjdp fill link_order */ 24733965Sjdp link_order->type = bfd_fill_link_order; 24833965Sjdp link_order->u.fill.value = 0; 24933965Sjdp } 25033965Sjdp else 25133965Sjdp { 25233965Sjdp link_order->type = bfd_indirect_link_order; 25333965Sjdp link_order->u.indirect.section = i; 25433965Sjdp ASSERT (i->output_section == output_section); 25533965Sjdp } 25633965Sjdp if (i->_cooked_size) 25733965Sjdp link_order->size = i->_cooked_size; 25833965Sjdp else 25933965Sjdp link_order->size = bfd_get_section_size_before_reloc (i); 26033965Sjdp link_order->offset = i->output_offset; 26133965Sjdp } 26233965Sjdp } 26333965Sjdp break; 26433965Sjdp 26533965Sjdp case lang_padding_statement_enum: 26633965Sjdp /* Make a new link_order with the right filler */ 26733965Sjdp { 26833965Sjdp asection *output_section; 26933965Sjdp struct bfd_link_order *link_order; 27033965Sjdp 27133965Sjdp output_section = statement->padding_statement.output_section; 27233965Sjdp ASSERT (statement->padding_statement.output_section->owner 27333965Sjdp == output_bfd); 27433965Sjdp if ((output_section->flags & SEC_HAS_CONTENTS) != 0) 27533965Sjdp { 27633965Sjdp link_order = bfd_new_link_order (output_bfd, output_section); 27733965Sjdp link_order->type = bfd_fill_link_order; 27833965Sjdp link_order->size = statement->padding_statement.size; 27933965Sjdp link_order->offset = statement->padding_statement.output_offset; 28033965Sjdp link_order->u.fill.value = statement->padding_statement.fill; 28133965Sjdp } 28233965Sjdp } 28333965Sjdp break; 28433965Sjdp 28533965Sjdp default: 28633965Sjdp /* All the other ones fall through */ 28733965Sjdp break; 28833965Sjdp } 28933965Sjdp} 29033965Sjdp 29133965Sjdp/* Call BFD to write out the linked file. */ 29233965Sjdp 29333965Sjdp 29433965Sjdp/**********************************************************************/ 29533965Sjdp 29633965Sjdp 29733965Sjdp/* Wander around the input sections, make sure that 29833965Sjdp we'll never try and create an output section with more relocs 29933965Sjdp than will fit.. Do this by always assuming the worst case, and 30033965Sjdp creating new output sections with all the right bits */ 30133965Sjdp#define TESTIT 1 30233965Sjdpstatic asection * 30333965Sjdpclone_section (abfd, s, count) 30433965Sjdp bfd *abfd; 30533965Sjdp asection *s; 30633965Sjdp int *count; 30733965Sjdp{ 30833965Sjdp#define SSIZE 8 30933965Sjdp char sname[SSIZE]; /* ?? find the name for this size */ 31033965Sjdp asection *n; 31133965Sjdp struct bfd_link_hash_entry *h; 31233965Sjdp /* Invent a section name - use first five 31333965Sjdp chars of base section name and a digit suffix */ 31433965Sjdp do 31533965Sjdp { 31633965Sjdp unsigned int i; 31733965Sjdp char b[6]; 31833965Sjdp for (i = 0; i < sizeof (b) - 1 && s->name[i]; i++) 31933965Sjdp b[i] = s->name[i]; 32033965Sjdp b[i] = 0; 32133965Sjdp sprintf (sname, "%s%d", b, (*count)++); 32233965Sjdp } 32333965Sjdp while (bfd_get_section_by_name (abfd, sname)); 32433965Sjdp 32533965Sjdp n = bfd_make_section_anyway (abfd, xstrdup (sname)); 32633965Sjdp 32733965Sjdp /* Create a symbol of the same name */ 32833965Sjdp 32933965Sjdp h = bfd_link_hash_lookup (link_info.hash, 33033965Sjdp sname, true, true, false); 33133965Sjdp h->type = bfd_link_hash_defined; 33233965Sjdp h->u.def.value = 0; 33333965Sjdp h->u.def.section = n ; 33433965Sjdp 33533965Sjdp 33633965Sjdp n->flags = s->flags; 33733965Sjdp n->vma = s->vma; 33833965Sjdp n->user_set_vma = s->user_set_vma; 33933965Sjdp n->lma = s->lma; 34033965Sjdp n->_cooked_size = 0; 34133965Sjdp n->_raw_size = 0; 34233965Sjdp n->output_offset = s->output_offset; 34333965Sjdp n->output_section = n; 34433965Sjdp n->orelocation = 0; 34533965Sjdp n->reloc_count = 0; 34633965Sjdp n->alignment_power = s->alignment_power; 34733965Sjdp return n; 34833965Sjdp} 34933965Sjdp 35033965Sjdp#if TESTING 35133965Sjdpstatic void 35233965Sjdpds (s) 35333965Sjdp asection *s; 35433965Sjdp{ 35533965Sjdp struct bfd_link_order *l = s->link_order_head; 35633965Sjdp printf ("vma %x size %x\n", s->vma, s->_raw_size); 35733965Sjdp while (l) 35833965Sjdp { 35933965Sjdp if (l->type == bfd_indirect_link_order) 36033965Sjdp { 36133965Sjdp printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename); 36233965Sjdp } 36333965Sjdp else 36433965Sjdp { 36533965Sjdp printf ("%8x something else\n", l->offset); 36633965Sjdp } 36733965Sjdp l = l->next; 36833965Sjdp } 36933965Sjdp printf ("\n"); 37033965Sjdp} 37133965Sjdpdump (s, a1, a2) 37233965Sjdp char *s; 37333965Sjdp asection *a1; 37433965Sjdp asection *a2; 37533965Sjdp{ 37633965Sjdp printf ("%s\n", s); 37733965Sjdp ds (a1); 37833965Sjdp ds (a2); 37933965Sjdp} 38033965Sjdp 38133965Sjdpstatic void 38233965Sjdpsanity_check (abfd) 38333965Sjdp bfd *abfd; 38433965Sjdp{ 38533965Sjdp asection *s; 38633965Sjdp for (s = abfd->sections; s; s = s->next) 38733965Sjdp { 38833965Sjdp struct bfd_link_order *p; 38933965Sjdp bfd_vma prev = 0; 39033965Sjdp for (p = s->link_order_head; p; p = p->next) 39133965Sjdp { 39233965Sjdp if (p->offset > 100000) 39333965Sjdp abort (); 39433965Sjdp if (p->offset < prev) 39533965Sjdp abort (); 39633965Sjdp prev = p->offset; 39733965Sjdp } 39833965Sjdp } 39933965Sjdp} 40033965Sjdp#else 40133965Sjdp#define sanity_check(a) 40233965Sjdp#define dump(a, b, c) 40333965Sjdp#endif 40433965Sjdp 40533965Sjdpstatic void 40633965Sjdpsplit_sections (abfd, info) 40733965Sjdp bfd *abfd; 40833965Sjdp struct bfd_link_info *info; 40933965Sjdp{ 41033965Sjdp asection *original_sec; 41133965Sjdp int nsecs = abfd->section_count; 41233965Sjdp sanity_check (abfd); 41333965Sjdp /* look through all the original sections */ 41433965Sjdp for (original_sec = abfd->sections; 41533965Sjdp original_sec && nsecs; 41633965Sjdp original_sec = original_sec->next, nsecs--) 41733965Sjdp { 41833965Sjdp boolean first = true; 41933965Sjdp int count = 0; 42033965Sjdp int lines = 0; 42133965Sjdp int relocs = 0; 42233965Sjdp struct bfd_link_order **pp; 42333965Sjdp bfd_vma vma = original_sec->vma; 42433965Sjdp bfd_vma shift_offset = 0; 42533965Sjdp asection *cursor = original_sec; 42633965Sjdp 42733965Sjdp /* count up the relocations and line entries to see if 42833965Sjdp anything would be too big to fit */ 42933965Sjdp for (pp = &(cursor->link_order_head); *pp; pp = &((*pp)->next)) 43033965Sjdp { 43133965Sjdp struct bfd_link_order *p = *pp; 43233965Sjdp int thislines = 0; 43333965Sjdp int thisrelocs = 0; 43433965Sjdp if (p->type == bfd_indirect_link_order) 43533965Sjdp { 43633965Sjdp asection *sec; 43733965Sjdp 43833965Sjdp sec = p->u.indirect.section; 43933965Sjdp 44033965Sjdp if (info->strip == strip_none 44133965Sjdp || info->strip == strip_some) 44233965Sjdp thislines = sec->lineno_count; 44333965Sjdp 44433965Sjdp if (info->relocateable) 44533965Sjdp thisrelocs = sec->reloc_count; 44633965Sjdp 44733965Sjdp } 44833965Sjdp else if (info->relocateable 44933965Sjdp && (p->type == bfd_section_reloc_link_order 45033965Sjdp || p->type == bfd_symbol_reloc_link_order)) 45133965Sjdp thisrelocs++; 45233965Sjdp 45333965Sjdp if (! first 45433965Sjdp && (thisrelocs + relocs > config.split_by_reloc 45533965Sjdp || thislines + lines > config.split_by_reloc 45633965Sjdp || config.split_by_file)) 45733965Sjdp { 45833965Sjdp /* create a new section and put this link order and the 45933965Sjdp following link orders into it */ 46033965Sjdp struct bfd_link_order *l = p; 46133965Sjdp asection *n = clone_section (abfd, cursor, &count); 46233965Sjdp *pp = NULL; /* Snip off link orders from old section */ 46333965Sjdp n->link_order_head = l; /* attach to new section */ 46433965Sjdp pp = &n->link_order_head; 46533965Sjdp 46633965Sjdp /* change the size of the original section and 46733965Sjdp update the vma of the new one */ 46833965Sjdp 46933965Sjdp dump ("before snip", cursor, n); 47033965Sjdp 47133965Sjdp n->_raw_size = cursor->_raw_size - l->offset; 47233965Sjdp cursor->_raw_size = l->offset; 47333965Sjdp 47433965Sjdp vma += cursor->_raw_size; 47533965Sjdp n->lma = n->vma = vma; 47633965Sjdp 47733965Sjdp shift_offset = l->offset; 47833965Sjdp 47933965Sjdp /* run down the chain and change the output section to 48033965Sjdp the right one, update the offsets too */ 48133965Sjdp 48233965Sjdp while (l) 48333965Sjdp { 48433965Sjdp l->offset -= shift_offset; 48533965Sjdp if (l->type == bfd_indirect_link_order) 48633965Sjdp { 48733965Sjdp l->u.indirect.section->output_section = n; 48833965Sjdp l->u.indirect.section->output_offset = l->offset; 48933965Sjdp } 49033965Sjdp l = l->next; 49133965Sjdp } 49233965Sjdp dump ("after snip", cursor, n); 49333965Sjdp cursor = n; 49433965Sjdp relocs = thisrelocs; 49533965Sjdp lines = thislines; 49633965Sjdp } 49733965Sjdp else 49833965Sjdp { 49933965Sjdp relocs += thisrelocs; 50033965Sjdp lines += thislines; 50133965Sjdp } 50233965Sjdp 50333965Sjdp first = false; 50433965Sjdp } 50533965Sjdp } 50633965Sjdp sanity_check (abfd); 50733965Sjdp} 50833965Sjdp/**********************************************************************/ 50933965Sjdpvoid 51033965Sjdpldwrite () 51133965Sjdp{ 51233965Sjdp /* Reset error indicator, which can typically something like invalid 51333965Sjdp format from openning up the .o files */ 51433965Sjdp bfd_set_error (bfd_error_no_error); 51533965Sjdp lang_for_each_statement (build_link_order); 51633965Sjdp 51733965Sjdp if (config.split_by_reloc || config.split_by_file) 51833965Sjdp split_sections (output_bfd, &link_info); 51933965Sjdp if (!bfd_final_link (output_bfd, &link_info)) 52033965Sjdp { 52133965Sjdp /* If there was an error recorded, print it out. Otherwise assume 52233965Sjdp an appropriate error message like unknown symbol was printed 52333965Sjdp out. */ 52433965Sjdp 52533965Sjdp if (bfd_get_error () != bfd_error_no_error) 52633965Sjdp einfo ("%F%P: final link failed: %E\n", output_bfd); 52733965Sjdp else 52833965Sjdp xexit(1); 52933965Sjdp } 53033965Sjdp} 531