1991-03-21 21:29:06 +00:00
|
|
|
/* Copyright (C) 1991 Free Software Foundation, Inc.
|
1993-12-11 22:55:55 +00:00
|
|
|
|
1991-03-21 21:29:06 +00:00
|
|
|
This file is part of GLD, the Gnu Linker.
|
|
|
|
|
1992-01-24 22:34:34 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
1991-03-21 21:29:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
1992-01-24 22:34:34 +00:00
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
1991-03-21 21:29:06 +00:00
|
|
|
|
1992-01-24 22:34:34 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
1991-03-21 21:29:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1992-01-24 22:34:34 +00:00
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This module writes out the final image by reading sections from the
|
|
|
|
input files, relocating them and writing them out
|
|
|
|
|
|
|
|
There are two main paths through this module, one for normal
|
1993-12-11 22:55:55 +00:00
|
|
|
operation and one for partial linking.
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
During normal operation, raw section data is read along with the
|
|
|
|
associated relocation information, the relocation info applied and
|
|
|
|
the section data written out on a section by section basis.
|
|
|
|
|
|
|
|
When partially linking, all the relocation records are read to work
|
|
|
|
out how big the output relocation vector will be. Then raw data is
|
|
|
|
read, relocated and written section by section.
|
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
Written by Steve Chamberlain sac@cygnus.com
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "bfd.h"
|
1992-01-24 22:34:34 +00:00
|
|
|
#include "sysdep.h"
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
#include "ld.h"
|
1993-12-11 22:55:55 +00:00
|
|
|
#include "ldexp.h"
|
|
|
|
#include "ldlang.h"
|
1991-03-21 21:29:06 +00:00
|
|
|
#include "ldwrite.h"
|
|
|
|
#include "ldmisc.h"
|
|
|
|
#include "ldsym.h"
|
1992-01-24 22:34:34 +00:00
|
|
|
#include "ldgram.h"
|
1993-12-11 22:55:55 +00:00
|
|
|
#include "ldmain.h"
|
|
|
|
#include "relax.h"
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
static void
|
1993-12-11 22:55:55 +00:00
|
|
|
read_relocs (abfd, section, symbols)
|
|
|
|
bfd * abfd;
|
|
|
|
asection * section;
|
|
|
|
asymbol ** symbols;
|
1991-03-21 21:29:06 +00:00
|
|
|
{
|
|
|
|
/* Work out the output section ascociated with this input section */
|
|
|
|
asection *output_section = section->output_section;
|
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
bfd_size_type reloc_size = bfd_get_reloc_upper_bound (abfd, section);
|
|
|
|
arelent **reloc_vector = (arelent **) ldmalloc (reloc_size);
|
1991-03-21 21:29:06 +00:00
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
if (bfd_canonicalize_reloc (abfd,
|
|
|
|
section,
|
|
|
|
reloc_vector,
|
|
|
|
symbols))
|
|
|
|
{
|
|
|
|
output_section->reloc_count += section->reloc_count;
|
|
|
|
}
|
1991-03-21 21:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
static void
|
|
|
|
setup_rel ()
|
1991-03-21 21:29:06 +00:00
|
|
|
{
|
|
|
|
/*
|
1993-12-11 22:55:55 +00:00
|
|
|
Run through each section of each file and work work out the total
|
|
|
|
number of relocation records which will finally be in each output
|
|
|
|
section
|
|
|
|
*/
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
LANG_FOR_EACH_INPUT_SECTION
|
1993-12-11 22:55:55 +00:00
|
|
|
(statement, abfd, section,
|
|
|
|
(read_relocs (abfd, section, statement->asymbols)));
|
1991-03-21 21:29:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1993-12-11 22:55:55 +00:00
|
|
|
Now run though all the output sections and allocate the space for
|
|
|
|
all the relocations
|
|
|
|
*/
|
1991-03-21 21:29:06 +00:00
|
|
|
LANG_FOR_EACH_OUTPUT_SECTION
|
1993-12-11 22:55:55 +00:00
|
|
|
(section,
|
1991-03-21 21:29:06 +00:00
|
|
|
(section->orelocation =
|
1993-12-11 22:55:55 +00:00
|
|
|
(arelent **) ldmalloc ((bfd_size_type) (sizeof (arelent **) *
|
|
|
|
section->reloc_count)),
|
|
|
|
section->reloc_count = 0));
|
1991-03-21 21:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1993-12-11 22:55:55 +00:00
|
|
|
ldwrite ()
|
1991-03-21 21:29:06 +00:00
|
|
|
{
|
1993-12-11 22:55:55 +00:00
|
|
|
PTR data_area = (PTR) ldmalloc (largest_section);
|
|
|
|
|
|
|
|
ldsym_write ();
|
|
|
|
|
1991-03-21 21:29:06 +00:00
|
|
|
if (config.relocateable_output == true)
|
1993-12-11 22:55:55 +00:00
|
|
|
setup_rel ();
|
|
|
|
|
|
|
|
write_relax (output_bfd, data_area, config.relocateable_output);
|
|
|
|
|
|
|
|
free (data_area);
|
|
|
|
|
1991-03-21 21:29:06 +00:00
|
|
|
/* Output the symbol table (both globals and locals). */
|
1992-01-24 22:34:34 +00:00
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
/* Print a map, if requested and possible. */
|
1992-01-24 22:34:34 +00:00
|
|
|
|
1993-12-11 22:55:55 +00:00
|
|
|
if (config.map_file)
|
|
|
|
{
|
1992-01-24 22:34:34 +00:00
|
|
|
ldsym_print_symbol_table ();
|
1993-12-11 22:55:55 +00:00
|
|
|
lang_map ();
|
1992-01-24 22:34:34 +00:00
|
|
|
}
|
1991-03-21 21:29:06 +00:00
|
|
|
}
|