Commit Graph

482 Commits

Author SHA1 Message Date
Andrew Burgess 14ea2c1b23 ld: Track changes to default region LMA even for empty sections
Given a linker script fragment like this:

   SECTIONS {
     . = 0x1000;
     .text   : AT(0x100) { *(.text)   }
     .data   : AT(0x200) { *(.data)   }
     .rodata : AT(0x300) { *(.rodata) }
   }

and an input file containing sections, '.text', '.data.1', and
'.rodata', then we'd expect the linker to place '.text' and '.rodata' in
the obvious way, and the '.data.1' orphan section would be located after
the '.data' section (assuming similar section properties).

Further, I believe that the expectation would be that the LMA for the
orphan '.data.1' section would start from 0x200 (as there is no '.data'
content).

However, right now, the LMA for '.data.1' would be 0x101, following on
from the '.text' section, this is because the change in LMA for the
'.data' section is not noticed by the linker, if there's no content in
the '.data' section.

What can be even more confusing to a user (though the cause is obvious
once you understand what's going on) is that adding some content to
'.data' will cause the orphan '.data.1' to switch to an LMA based off of
0x200.

This commit changes the behaviour so that an empty section that is in
the default lma region, and sets its lma, will adjust the lma of the
default region, this change will then be reflected in following sections
within the default lma memory region.

There's a new test to cover this issue that passes on a range of
targets, however, some targets generate additional sections, or have
stricter memory region size requirements that make it harder to come
up with a generic pass pattern, that still tests the required
features.  For now I've set the test to ignore these targets.

ld/ChangeLog:

	* ldlang.c (lang_size_sections_1): Shortcut loop only after
	tracking changes to the default regions LMA.
	* testsuite/ld-elf/orphan-9.ld: Extend header comment.
	* testsuite/ld-elf/orphan-10.d: New file.
	* testsuite/ld-elf/orphan-10.s: New file.
	* NEWS: Mention change in behaviour.
2017-03-09 21:11:06 +00:00
Andrew Burgess 77f5e65ecf ld: better handling of lma region for orphan sections
When picking an lma_region for an orphan section we currently create a
new lang_output_section_statement_type and then populate this with the
orphan section.

The problem is that the lang_output_section_statement_type has a prev
pointer that links back to the previous output section.  For non-orphan
output sections, that are created in linker script order, the prev
pointer will point to the output section that appears previous in linker
script order, as you'd probably expect.

The problem is that orphan sections are placed after processing the
linker script, and so, in the case of an output section created for an
orphan input section, the prev pointer actually points to the last
output section created.

This causes some unexpected behaviour when the orphan section is not
placed after the last non-orphan section that was created.

For example, consider this linker script:

  MEMORY {
    TEXT   : ORIGIN = 0x200,  LENGTH = 0x10
    RODATA : ORIGIN = 0x400,  LENGTH = 0x10
  }

  SECTIONS {
    .text   :           {*(.text)    } AT>TEXT
    .data   : AT(0x300) { *(.data)   }
    .rodata :           { *(.rodata) } AT>RODATA
  }

If we are processing an orphan section '.data.1' and decide to place
this after '.data', then the output section created will have a prev
pointer that references the '.rodata' output section.  The result of
this is that '.data.1' will actually be assigned to the RODATA lma
region, which is probably not the expected behaviour.

The reason why '.data.1' is placed into the lma region of the '.rodata'
section is that lma region propagation is done at the time we create the
output section, based on the previous output section pointer, which is
really just a last-output-section-created pointer at that point in time,
though the prev point is fixed up later to reflect the true order of the
output sections.

The solution I propose in this commit is to move the propagation of lma
regions into a separate pass of the linker, rather than performing this
as part of the enter/exit of output sections during linker script
parsing.

During this later phase we have all of the output sections to hand, and
the prev/next points have been fixed up by this point to reflect the
actual placement ordering.

There's a new test to cover this issue that passes on a range of
targets, however, some targets generate additional sections, or have
stricter memory region size requirements that make it harder to come
up with a generic pass pattern, that still tests the required
features.  For now I've set the test to ignore these targets.

ld/ChangeLog:

	* ldlang.c (lang_leave_output_section_statement): Move lma_region
	logic to...
	(lang_propagate_lma_regions): ...this new function.
	(lang_process): Call new function.
	* testsuite/ld-elf/orphan-9.d: New file.
	* testsuite/ld-elf/orphan-9.ld: New file.
	* testsuite/ld-elf/orphan-9.s: New file.
	* NEWS: Mention change in behaviour.
2017-03-09 21:11:05 +00:00
Alan Modra d933941d6c Fix ld uninitialized read of script ASSERT data structure
lang_assignment_statement serves both assignments and asserts.

	* ldlang.c (open_input_bfds): Check that lang_assignment_statement
	is not an assert before referencing defsym.
2017-03-07 12:33:28 +10:30
Nick Clifton 1273da0414 Fix buffer overflows in ld when printing translated strings.
ld	* ldmain.c (add_archive_element): Eliminate string buffer.
	* ldlang.c (lang_print_asneeded): Likewise.
2017-01-31 12:07:04 +00:00
Alan Modra 2571583aed Update year range in copyright notice of all files. 2017-01-02 14:08:56 +10:30
Igor Kudrin 9ef7906f20 Do not add padding if an output section is marked as ignored
* ldlang.c (size_input_section): Avoid calling insert_pad
	if output_section_statement->ignored is set.
2016-12-22 00:12:36 +10:30
Ambrogino Modigliani 370dfff4c4 Fix spelling in comments in C source files (ld)
* deffile.h: Fix spelling in comments.
	* ld.h: Fix spelling in comments.
	* ldlang.c: Fix spelling in comments.
	* ldmisc.c: Fix spelling in comments.
	* pe-dll.c: Fix spelling in comments.
2016-11-27 15:04:38 +10:30
Andrew Burgess 8f1732fc2a ld: Allow EXCLUDE_FILE to be used outside of the section list
Currently the EXCLUDE_FILE linker script construct can only be used
within the input section list, and applied only to the section pattern
immediately following the EXCLUDE_FILE.  For example:

    *.o (EXCLUDE_FILE (a.o) .text .rodata)

In this case all sections matching '.text' are included from all files
matching '*.o' but not from the file 'a.o'.  All sections matching
'.rodata' are also included from all files matching '*.o' (incluing from
'a.o').

If the user wants to restrict the inclusion of section '.rodata' so that
this too is not taken from the file 'a.o' then the above example must be
extended like this:

    *.o (EXCLUDE_FILE (a.o) .text EXCLUDE_FILE (a.o) .rodata)

However, due to the internal grammar of the linker script language the
snippet 'EXCLUDE_FILE (a.o) .text' is parsed by a pattern called
'wildcard_spec'.  The same 'wildcard_spec' pattern is also used to parse
the input file name snippet '*.o' in the above examples.  As a result of
this pattern reuse within the linker script grammar then the following
is also a valid linker script construct:

    EXCLUDE_FILE (a.o) *.o (.text .rodata)

However, though the linker accepts this without complaint the
EXCLUDE_FILE part is silently ignored and has no effect.

This commit takes this last example and makes it a useful, valid,
construct.  The last example now means to include sections '.text' and
'.rodata' from all files matching '*.o' except for the file 'a.o'.

If the list of input sections is long, and the user knows that the file
exclusion applies across the list then the second form might be a
clearer alternative to replicating the EXCLUDE_FILE construct.

I've added a set of tests for EXCLUDE_FILE to the linker, including
tests for the new functionality.

ld/ChangeLog:

	* ldlang.h (struct lang_wild_statement_struct): Add
	exclude_name_list field.
	* ldlang.c (walk_wild_file_in_exclude_list): New function.
	(walk_wild_consider_section): Use new
	walk_wild_file_in_exclude_list function.
	(walk_wild_file): Add call to walk_wild_file_in_exclude_list.
	(print_wild_statement): Print new exclude_name_list field.
	(lang_add_wild): Initialise new exclude_name_list field.
	* testsuite/ld-scripts/exclude-file-1.d: New file.
	* testsuite/ld-scripts/exclude-file-1.map: New file.
	* testsuite/ld-scripts/exclude-file-1.t: New file.
	* testsuite/ld-scripts/exclude-file-2.d: New file.
	* testsuite/ld-scripts/exclude-file-2.map: New file.
	* testsuite/ld-scripts/exclude-file-2.t: New file.
	* testsuite/ld-scripts/exclude-file-3.d: New file.
	* testsuite/ld-scripts/exclude-file-3.map: New file.
	* testsuite/ld-scripts/exclude-file-3.t: New file.
	* testsuite/ld-scripts/exclude-file-4.d: New file.
	* testsuite/ld-scripts/exclude-file-4.map: New file.
	* testsuite/ld-scripts/exclude-file-4.t: New file.
	* testsuite/ld-scripts/exclude-file-a.s: New file.
	* testsuite/ld-scripts/exclude-file-b.s: New file.
	* testsuite/ld-scripts/exclude-file.exp: New file.
	* ld.texinfo (Input Section Basics): Update description of
	EXCLUDE_FILE to cover the new features.
	* NEWS: Mention new EXCLUDE_FILE usage.
2016-11-04 12:04:11 +00:00
Alan Modra f02cb05882 Always descend into output section statements in lang_do_assignments
See https://sourceware.org/ml/binutils/2016-07/msg00091.html
This patch stop --gc-sections elf_gc_sweep_symbol localizing symbols
that ought to remain global.

The difficulty with always descending into output section statements
is that symbols defined by the script in such statements don't have
a bfd section when lang_do_assignments runs early in the link process.
There are two approaches to curing this problem.  Either we can
create the bfd section early, or we can use a special section.  This
patch takes the latter approach and uses bfd_und_section.  (Creating
bfd sections early results in changed output section order, and thus
lots of testsuite failures.  You can't create all output sections
early to ensure proper ordering as KEEP then stops empty sections
from being stripped.)

The wrinkle with this approach is that some code that runs at
gc-sections time needs to be made aware of the odd defined symbols
using bfd_und_section.

bfd/
	* elf64-x86-64.c (elf_x86_64_convert_load_reloc): Handle symbols
	defined temporarily with bfd_und_section.
	* elflink.c (_bfd_elf_gc_keep): Don't set SEC_KEEP for bfd_und_section.
	* elfxx-mips.c (mips_elf_local_pic_function_p): Exclude defined
	symbols with bfd_und_section.
ld/
	* ldlang.c (lang_do_assignments_1): Descend into output section
	statements that do not yet have bfd sections.  Set symbol section
	temporarily for symbols defined in such statements to the undefined
	section.  Don't error on data or reloc statements until final phase.
	* ldexp.c (exp_fold_tree_1 <etree_assign>): Handle bfd_und_section
	in expld.section.
	* testsuite/ld-mmix/bpo-10.d: Adjust.
	* testsuite/ld-mmix/bpo-11.d: Adjust.
2016-10-11 18:13:04 +10:30
Alan Modra 50e03d47b7 Pass link_info to _bfd_merge_private_bfd_data
Most BFD linker functions take a bfd_link_info param, which reinforces
the fact that they are linker functions and allow access to linker
callbacks, eg. einfo for printing errors.  I was going to use einfo
for --fatal-warnings support before I decided a better way was the
patch commit 4519d071.

bfd/
	* targets.c (bfd_target <_bfd_merge_private_bfd_data>): Replace
	obfd param with struct bfd_link_info param.  Update all callers.
	* linker.c (bfd_merge_private_bfd_data): Likewise.
	(_bfd_generic_verify_endian_match): Likewise.
	* aoutf1.h (sunos_merge_private_bfd_data): Likewise.
	* coff-arm.c (coff_arm_merge_private_bfd_data): Likewise.
	* elf-attrs.c (_bfd_elf_merge_object_attributes): Likewise.
	* elf-bfd.h (_bfd_elf_ppc_merge_fp_attributes): Likewise.
	(_bfd_elf_merge_object_attributes): Likewise.
	* elf-m10300.c (_bfd_mn10300_elf_merge_private_bfd_data): Likewise.
	* elf-s390-common.c (elf_s390_merge_obj_attributes): Likewise.
	* elf32-arc.c (arc_elf_merge_private_bfd_data): Likewise.
	* elf32-arm.c (elf32_arm_merge_eabi_attributes): Likewise.
	(elf32_arm_merge_private_bfd_data): Likewise.
	* elf32-bfin.c (elf32_bfin_merge_private_bfd_data): Likewise.
	* elf32-cr16.c (_bfd_cr16_elf_merge_private_bfd_data): Likewise.
	* elf32-cris.c (cris_elf_merge_private_bfd_data): Likewise.
	* elf32-frv.c (frv_elf_merge_private_bfd_data): Likewise.
	* elf32-h8300.c (elf32_h8_merge_private_bfd_data): Likewise.
	* elf32-i370.c (i370_elf_merge_private_bfd_data): Likewise.
	* elf32-iq2000.c (iq2000_elf_merge_private_bfd_data): Likewise.
	* elf32-m32c.c (m32c_elf_merge_private_bfd_data): Likewise.
	* elf32-m32r.c (m32r_elf_merge_private_bfd_data): Likewise.
	* elf32-m68hc1x.c (_bfd_m68hc11_elf_merge_private_bfd_data): Likewise.
	* elf32-m68hc1x.h (_bfd_m68hc11_elf_merge_private_bfd_data): Likewise.
	* elf32-m68k.c (elf32_m68k_merge_private_bfd_data): Likewise.
	* elf32-mcore.c (mcore_elf_merge_private_bfd_data): Likewise.
	* elf32-mep.c (mep_elf_merge_private_bfd_data): Likewise.
	* elf32-msp430.c (elf32_msp430_merge_mspabi_attributes): Likewise.
	(elf32_msp430_merge_private_bfd_data): Likewise.
	* elf32-mt.c (mt_elf_merge_private_bfd_data): Likewise.
	* elf32-nds32.c (nds32_elf_merge_private_bfd_data): Likewise.
	* elf32-nios2.c (nios2_elf32_merge_private_bfd_data): Likewise.
	* elf32-or1k.c (elf32_or1k_merge_private_bfd_data): Likewise.
	* elf32-ppc.c (_bfd_elf_ppc_merge_fp_attributes): Likewise.
	(ppc_elf_merge_obj_attributes): Likewise.
	(ppc_elf_merge_private_bfd_data): Likewise.
	* elf32-rl78.c (rl78_elf_merge_private_bfd_data): Likewise.
	* elf32-rx.c (rx_elf_merge_private_bfd_data): Likewise.
	* elf32-s390.c (elf32_s390_merge_private_bfd_data): Likewise.
	* elf32-score.c (s3_elf32_score_merge_private_bfd_data): Likewise.
	(elf32_score_merge_private_bfd_data): Likewise.
	* elf32-score.h (s7_elf32_score_merge_private_bfd_data): Likewise.
	* elf32-score7.c (s7_elf32_score_merge_private_bfd_data): Likewise.
	* elf32-sh.c (sh_merge_bfd_arch, sh_elf_merge_private_data): Likewise.
	* elf32-sh64.c (sh64_elf_merge_private_data): Likewise.
	* elf32-sparc.c (elf32_sparc_merge_private_bfd_data): Likewise.
	* elf32-tic6x.c (elf32_tic6x_merge_attributes): Likewise.
	(elf32_tic6x_merge_private_bfd_data): Likewise.
	* elf32-v850.c (v850_elf_merge_private_bfd_data): Likewise.
	* elf32-vax.c (elf32_vax_merge_private_bfd_data): Likewise.
	* elf32-visium.c (visium_elf_merge_private_bfd_data): Likewise.
	* elf32-xtensa.c (elf_xtensa_merge_private_bfd_data): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_merge_private_bfd_data): Likewise.
	* elf64-ppc.c (ppc64_elf_merge_private_bfd_data): Likewise.
	* elf64-s390.c (elf64_s390_merge_private_bfd_data): Likewise.
	* elf64-sh64.c (sh_elf64_merge_private_data): Likewise.
	* elf64-sparc.c (elf64_sparc_merge_private_bfd_data): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_merge_private_bfd_data): Likewise.
	* elfnn-ia64.c (elfNN_ia64_merge_private_bfd_data): Likewise.
	* elfxx-mips.c (mips_elf_merge_obj_e_flags): Likewise.
	(mips_elf_merge_obj_attributes): Likewise.
	(_bfd_mips_elf_merge_private_bfd_data): Likewise.
	* elfxx-mips.h (_bfd_mips_elf_merge_private_bfd_data): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_merge_private_bfd_data): Likewise.
	* elfxx-sparc.h (_bfd_sparc_elf_merge_private_bfd_data): Likewise.
	* elfxx-target.h (bfd_elfNN_bfd_merge_private_bfd_data): Likewise.
	* elfxx-tilegx.c (_bfd_tilegx_elf_merge_private_bfd_data): Likewise.
	* elfxx-tilegx.h (_bfd_tilegx_elf_merge_private_bfd_data): Likewise.
	* libbfd-in.h (_bfd_generic_bfd_merge_private_bfd_data): Likewise.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
ld/
	* ldlang.c (lang_check): Update bfd_merge_private_bfd_data call.
2016-10-07 11:24:41 +10:30
Alan Modra 52d45da3f2 Make bfd_error_handler_type like vprintf
It was like printf, which means you can't use bfd_set_error_handler to
hook in a function to do something and then call the original handler.

The patch also deletes some unused functions and makes pointers local.

bfd/
	* bfd-in.h: Include stdarg.h.
	* bfd.c (bfd_error_handler_type): Make like vprintf.
	(_bfd_error_internal): Rename from _bfd_error_handler.  Make static.
	(error_handler_internal): New function, split out from..
	(_bfd_default_error_handler): ..here.  Rename to _bfd_error_handler.
	(bfd_set_error_handler): Update.
	(bfd_get_error_handler, bfd_get_assert_handler): Delete.
	(_bfd_assert_handler): Make static.
	* coffgen.c (null_error_handler): Update params.
	* elf-bfd.h (struct elf_backend_data <link_order_error_handler>):
	Don't use bfd_error_handler_type.
	* elf64-mmix.c (mmix_dump_bpo_gregs): Likewise.
	* elfxx-target.h (elf_backend_link_order_error_handler): Default
	to _bfd_error_handler.
	* libbfd-in.h (_bfd_default_error_handler): Don't declare.
	(bfd_assert_handler_type): Likewise.
	(_bfd_error_handler): Update.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
ld/
	* ldlang.c (ignore_bfd_errors): Update params.
2016-09-30 11:38:39 +09:30
Alan Modra 2e84f9c1b5 Ignore symbols defined in SHF_EXCLUDE sections
PR 20513
	* ldlang.c (section_already_linked): Deal with SHF_EXCLUDE sections.
2016-08-31 19:22:43 +09:30
Alan Modra 4212b42d79 Don't include libbfd.h outside of bfd, part 4
Not much to see here, just renaming a function.

bfd/
	* targets.c (bfd_seach_for_target): Rename to..
	(bfd_iterate_over_targets): ..this.  Rewrite doc.
	* bfd-in2.h: Regenerate.
ld/
	* ldlang.c (open_output): Replace bfd_search_for_target with
	bfd_iterate_over_targets.  Localize vars.
2016-07-16 13:29:35 +09:30
Alan Modra 76e7a75123 Don't include libbfd.h outside of bfd, part 1
Make BFD_ALIGN available to objcopy.  Fix assertions.  Don't use
bfd_log2 in ppc32elf.em or bfd_malloc in xtensaelf.em and bucomm.c.

bfd/
	* libbfd-in.h (BFD_ALIGN): Move to..
	* bfd-in.h: ..here.
	* elf32-ppc.h (struct ppc_elf_params): Add pagesize.
	* elf32-ppc.c (default_params): Adjust init.
	(ppc_elf_link_params): Set pagesize_p2.
	* libbfd.h: Regenerate.
	* bfd-in2.h: Regenerate.
binutils/
	* ar.c: Don't include libbfd.h.
	* objcopy.c: Likewise.
	* bucomm.c (bfd_get_archive_filename): Use xmalloc rather than
	bfd_malloc.
gas/
	* config/bfin-parse.y: Don't include libbfd.h.
	* config/tc-bfin.c: Likewise.
	* config/tc-rl78.c: Likewise.
	* config/tc-rx.c: Likewise.
	* config/tc-metag.c: Likewise.
	(create_dspreg_htabs, create_scond_htab): Use gas_assert not BFD_ASSERT.
	* Makefile.am: Update dependencies.
	* Makefile.in: Regenerate.
ld/
	* ldlang.c: Don't include libbfd.h.
	* emultempl/nds32elf.em: Likewise.
	* emultempl/ppc64elf.em: Likewise.
	* emultempl/ppc32elf.em: Likewise.
	(pagesize): Delete.
	(params): Update init.
	(ppc_after_open_output): Use params.pagesize.  Don't call bfd_log2.
	(PARSE_AND_LIST_ARGS_CASES): Use params.pagesize.
	* emultempl/sh64elf.em: Don't include libbfd.h.
	(after_allocation): Use ASSERT, not BFD_ASSERT.
	* emultempl/xtensaelf.em: Don't include libbfd.h.
	(replace_insn_sec_with_prop_sec): Use xmalloc, not bfd_malloc.
	* Makefile.am: Update dependencies.
	* Makefile.in: Regenerate.
2016-07-16 13:25:11 +09:30
Alan Modra 0aa7f5862e Formatting fixes.
* ldbuildid.c: Formatting.
	* ldcref.c: Formatting.
	* ldctor.c: Formatting.
	* ldemul.c: Formatting.
	* ldexp.c: Formatting.
	* ldfile.c: Formatting.
	* ldlang.c: Formatting.
	* ldmain.c: Formatting.
	* ldwrite.c: Formatting.
2016-06-14 13:25:21 +09:30
Alan Modra 3860d2b4b7 Delete bfd_my_archive macro
Many more places use abfd->my_archive rather than bfd_my_archive (abfd),
so let's make the code consistently use the first idiom.

bfd/
	* bfd-in.h (bfd_my_archive): Delete.
	* bfd-in2.h: Regenerate.
binutils/
	* ar.c: Expand uses of bfd_my_archive.
	* size.c: Likewise.
ld/
	* ldlang.c: Expand uses of bfd_my_archive.
	* ldmain.c: Likewise.
	* ldmisc.c: Likewise.
	* plugin.c: Likewise.
2016-06-14 13:24:37 +09:30
H.J. Lu d4758f96f7 Pass %E to einfo when bfd_section == NULL
Report BFD error When bfd_section == NULL in init_os.

	* ldlang.c (init_os): Pass %E to einfo when bfd_section == NULL.
2016-05-04 19:41:34 -07:00
Nick Clifton 4f3b23b390 Add support for non-ELF targets to check their relocs.
bfd	* aout-adobe.c: Use _bfd_generic_link_check_relocs.
	* aout-target.h: Likewise.
	* aout-tic30.c: Likewise.
	* binary.c: Likewise.
	* bout.c: Likewise.
	* coff-alpha.c: Likewise.
	* coff-rs6000.c: Likewise.
	* coff64-rs6000.c: Likewise.
	* coffcode.h: Likewise.
	* i386msdos.c: Likewise.
	* i386os9k.c: Likewise.
	* ieee.c: Likewise.
	* ihex.c: Likewise.
	* libbfd-in.h: Likewise.
	* libecoff.h: Likewise.
	* mach-o-target.c: Likewise.
	* mmo.c: Likewise.
	* nlm-target.h: Likewise.
	* oasys.c: Likewise.
	* pef.c: Likewise.
	* plugin.c: Likewise.
	* ppcboot.c: Likewise.
	* som.c: Likewise.
	* srec.c: Likewise.
	* tekhex.c: Likewise.
	* versados.c: Likewise.
	* vms-alpha.c: Likewise.
	* xsym.c: Likewise.
	* elfxx-target.h: Use _bfd_elf_link_check_relocs.
	* linker.c (bfd_link_check_relocs): New function.
	(_bfd_generic_link_check_relocs): New function.
	* targets.c (BFD_JUMP_TABLE_LINK): Add initialization of
	_bfd_link_check_relocs field.
	(struct bfd_target)L Add _bfd_link_check_relocs field.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.

ld	* ldlang.c (lang_check_relocs): Use bfd_link_check_relocs in
	prefernce to _bfd_elf_link_check_relocs.  Drop test for ELF
	targets.  Do not stop the checks when problems are encountered.

include	* bfdlink.h: Add prototype for bfd_link_check_relocs.
2016-04-21 15:43:00 +01:00
H.J. Lu fbf05aa70d Move ELF relocation check after lang_gc_sections
Move ELF relocation check after lang_gc_sections so that all the
reference counting code for plt and got relocs can be removed.  This
only affects ELF targets which check relocations after opening all
input file.

	* ldlang.c (lang_check_relocs): New function.
	(lang_process): Call lang_check_relocs after lang_gc_sections.
	* emultempl/elf32.em (gld${EMULATION_NAME}_before_parse): Don't
	call _bfd_elf_link_check_relocs here.
2016-04-20 17:08:55 -07:00
Matthew Fortune cdf969539c Add new NOCROSSREFS_TO linker script command
NOCROSSREFS_TO is similar to the existing NOCROSSREFS command but only
checks one direction of cross referencing.

ld/ChangeLog

	* ld.texinfo: Document NOCROSSREFS_TO script command.
	* ldlang.h (struct lang_nocrossrefs): Add onlyfirst field.
	(lang_add_nocrossref_to): New prototype.
	* ldcref.c (check_local_sym_xref): Use onlyfirst to only look for
	symbols defined in the first section.
	(check_nocrossref): Likewise.
	* ldgram.y (NOCROSSREFS_TO): New script command.
	* ldlang.c (lang_add_nocrossref): Set onlyfirst to FALSE.
	(lang_add_nocrossref_to): New function.
	* ldlex.l (NOCROSSREFS_TO): New token.
	* NEWS: Mention NOCROSSREFS_TO.
	* testsuite/ld-scripts/cross4.t: New file.
	* testsuite/ld-scripts/cross5.t: Likewise.
	* testsuite/ld-scripts/cross6.t: Likewise.
	* testsuite/ld-scripts/cross7.t: Likewise.
	* testsuite/ld-scripts/crossref.exp: Run 4 new NOCROSSREFS_TO
	tests.
2016-04-18 12:45:46 +01:00
Alan Modra 136a43b762 PR18452, ld allows overlapping sections
PR 18452
	* ldlang.c (maybe_overlays): Delete.
	(lang_size_sections_1): Remove code setting maybe_overlays.
	(lang_check_section_addresses): Instead detect overlays by
	exact match of section VMAs here.  Fix memory leak.
2016-04-08 23:41:44 +09:30
Dan Gisselquist 953dd97e56 Show sizes in linker map in target machine address units
* ldlang.c (print_output_section_statement): Show minfo size
	in target machine address units.
	(print_reloc_statement): Likewise.
	(print_padding_statement): Likewise.
	(print_data_statement): Likewise.  Ensure minimum print_dot
	increment of one address unit.
2016-04-08 14:33:02 +09:30
Alan Modra fc90c28021 More cases of missing TO_ADDR in ldlang.c
and one extraneous occurrence.

	* ldlang.c (TO_ADDR, TO_SIZE, opb_shift): Move earlier in file.
	(lang_insert_orphan): Use TO_ADDR in __stop sym calculation.
	(print_input_section): Don't use TO_ADDR when printing section
	size.
	(lang_size_sections_1): Use TO_ADDR in overlay lma calculation.
	(lang_size_sections): Use TO_ADDR in relro end calculation.
2016-03-31 19:35:33 +10:30
Alan Modra a87dd97a20 PR18452, ld allows overlapping sections
PR 18452
	* ldlang.c (maybe_overlays): New static var.
	(lang_size_sections_1): Set it here.
	(struct check_sec): New.
	(sort_sections_by_lma): Adjust for array of structs.
	(sort_sections_by_vma): New function.
	(lang_check_section_addresses): Check both LMA and VMA for overlap.
	* testsuite/ld-scripts/rgn-over7.d: Adjust.
2016-03-30 17:50:03 +10:30
Alan Modra ca62bc4a86 Missing TO_ADDR
* ldlang.c (lang_size_sections_1): Correct code detecting a
	backward non-overlapping move.
2016-03-30 17:50:03 +10:30
Alan Modra 2e4a7aea70 Use consistent test for .tbss sections in ldlang.c
Some places tested SEC_LOAD, others SEC_HAS_CONTENTS.

	* ldlang.c (IS_TBSS): New macro, extracted from..
	(IGNORE_SECTION): ..here.
	(lang_size_sections_1): Use IS_TBSS and IGNORE_SECTION.
	(lang_size_sections, lang_do_assignments_1): Use IS_TBSS.
2016-03-30 17:50:03 +10:30
Nick Clifton 4153b6dbb0 Improve COFF/PE linker garbage collection by preventing the removal of sections containing exported symbols.
PR ld/19803
	* ldlang.c (lang_add_gc_name): New function.  Adds the provided
	symbol name to the list of gc symbols.
	(lang_process): Call lang_add_gc_name with entry_symbol_default if
	entry_symbol.name is NULL.  Use lang_add_gc_name to add the init
	and fini function names.
	* pe-dll.c (process_def_file_and_drectve): Add exported names to
	the gc symbol list.
	* testsuite/ld-pe/pr19803.s: Do not export _testval symbol.
	* testsuite/ld-pe/pr19803.d: Tweak expected output.
2016-03-22 12:25:08 +00:00
Senthil Kumar Selvaraj 41f46ed9fe Fix possible failure in the AVR linker tests.
* ld-avr/gc-section-debugline.d: Relax regex check for CU.
2016-03-18 09:51:47 +00:00
Alan Modra 6f2750feaf Copyright update for binutils 2016-01-01 23:00:01 +10:30
Alan Modra b39910205f Account for .tbss alignment when adjusting start of relro
Another option might be to not bump "dot" for .tbss alignment in the
main section sizing loop, but that could leak some of the following
section into the TLS segment.  Leakage shouldn't matter since it will
be to bytes past the end of .tdata, but for now this is a safer
option.

	PR ld/19264
	* ldlang.c (lang_size_sections): Don't ignore .tbss when
	adjusting start of relro region.
2015-11-19 16:22:25 +10:30
Alan Modra 936384714f Re: Orphan output section with multiple input sections
The last patch missed handling the case where the ideal place to put
an orphan was after a non-existent output section statement, as can
happen when not using the builtin linker scripts.  This patch uses the
updated flags for that case too, and extends the support to mmo and pe.

	PR ld/19162
	* emultempl/elf32.em (gld${EMULATION_NAME}_place_orphan): Pass
	updated flags to lang_output_section_find_by_flags.
	* emultempl/mmo.em (mmo_place_orphan): Merge flags for any
	other input sections that might match a new output section to
	decide placement.
	* emultempl/pe.em (gld_${EMULATION_NAME}_place_orphan): Likewise.
	* emultempl/pep.em (gld_${EMULATION_NAME}_place_orphan): Likewise.
	* ldlang.c (lang_output_section_find_by_flags): Add sec_flags param.
	* ldlang.h (lang_output_section_find_by_flags): Update prototype.
2015-10-29 19:42:50 +10:30
Alan Modra 321df0656c Evaluate __start_* and __stop_* symbol PROVIDE expressions early
Makes these symbols defined before bfd_elf_size_dynamic_sections, to
avoid horrible hacks elsewhere.  The exp_fold_tree undefweak change
is necessary to define undefweak symbols early too.  The comment was
wrong.  PROVIDE in fact defines undefweak symbols, via
bfd_elf_record_link_assignment.

	PR ld/19175
	* ldlang.c (lang_insert_orphan): Evaluate __start_* and __stop_*
	symbol PROVIDE expressions.
	* ldexp.c (exp_fold_tree_1 <etree_provide>): Define undefweak
	references.
2015-10-27 12:54:59 +10:30
Alan Modra 975f8a9e31 Delay converting linker script defined symbols from absolute
Giving linker script symbols defined outside of output sections a
section-relative value early, leads to them being used in expressions
as if they were defined inside an output section.  This can mean loss
of the section VMA, and wrong results.

ld/
	PR ld/18963
	* ldexp.h (struct ldexp_control): Add rel_from_abs.
	(ldexp_finalize_syms): Declare.
	* ldexp.c (new_rel_from_abs): Keep absolute for expressions
	outside of output section statements.  Set rel_from_abs.
	(make_abs, exp_fold_tree, exp_fold_tree_no_dot): Clear rel_from_abs.
	(struct definedness_hash_entry): Add final_sec, and comment.
	(update_definedness): Set final_sec.
	(set_sym_sections, ldexp_finalize_syms): New functions.
	* ldlang.c (lang_process): Call ldexp_finalize_syms.
ld/testsuite
	PR ld/18963
	* ld-scripts/pr18963.d,
	* ld-scripts/pr18963.t: New test.
	* ld-scripts/expr.exp: Run it.
	* ld-elf/provide-hidden-2.ld: Explicitly make "dot" absolute.
	* ld-mips-elf/gp-hidden.sd: Don't care about _gp section.
	* ld-mips-elf/no-shared-1-n32.d: Don't care about symbol shown at
	start of .data section.
	* ld-mips-elf/no-shared-1-n64.d: Likewise.
	* ld-mips-elf/no-shared-1-o32.d: Likewise.
2015-09-18 12:39:16 +09:30
Andrew Burgess c005eb9e34 ld: Extend options for altering orphan handling behaviour.
Replace the options --warn-orphan and --no-warn-orphan with a single
option --orphan-handling=MODE, where mode can be place, warn, error, and
discard.

Mode 'place' is the default, and is the current behaviour, placing the
orphan section into a suitable output section.

Mode 'warn' is the same as '--warn-orphan'.  The orphan is also placed
using the same algorithm as for 'place'.

Mode 'error' is the same as '--warn-orphan' and '--fatal-warnings'.

Mode 'discard' assigns all output sections to the /DISCARD/ section.

ld/ChangeLog:

	* ld.h (enum orphan_handling_enum): New.
	(ld_config_type): Remove warn_orphan, add orphan_handling.
	* ldemul.c (ldemul_place_orphan): Remove warning about orphan
	sections.
	* ldlang.c (ldlang_place_orphan): New function.
	(lang_place_orphans): Call ldlang_place_orphan.
	* ldlex.h (enum option_values): Remove OPTION_WARN_ORPHAN and
	OPTION_NO_WARN_ORPHAN, add OPTION_ORPHAN_HANDLING.
	* lexsup.c (ld_options): Remove 'warn-orphan' and
	'no-warn-orphan', add 'orphan-handling'.
	(parse_args): Remove handling for OPTION_WARN_ORPHAN and
	OPTION_NO_WARN_ORPHAN, add handling for OPTION_ORPHAN_HANDLING.
	* NEWS: Replace text about --warn-orphan with --orphan-handling.
	* ld.texinfo (Options): Remove --warn-orphan entry and add
	entry on --orphan-handling.
	(Orphan Sections): Add reference to relevant command line options.

ld/testsuite/ChangeLog:

	* ld-elf/elf.exp: Switch to rely on run_dump_test.
	* ld-elf/orphan-5.l: Update expected output.
	* ld-elf/orphan-5.d: New file.
	* ld-elf/orphan-6.d: New file.
	* ld-elf/orphan-6.l: New file.
	* ld-elf/orphan-7.d: New file.
	* ld-elf/orphan-7.map: New file.
	* ld-elf/orphan-8.d: New file.
	* ld-elf/orphan-8.map: New file.
2015-09-04 22:30:55 +01:00
H.J. Lu 0e1862bb40 Add output_type to bfd_link_info
The "shared" field in bfd_link_info is set for both DSO and and PIE.
There are separate fields for executable and relocatable outputs.  This
patch adds an "output_type" field:

enum output_type
{
  type_unknown = 0,
  type_executable,
  type_dll,
  type_relocatable
};

and a "pic" field to bfd_link_info to replace shared, executable and
relocatable fields so that we can use the "output_type" field to check
for output type and the "pic" field check if output is PIC.  Macros,
bfd_link_executable, bfd_link_dll, bfd_link_relocatable, bfd_link_pic
and bfd_link_pie, are provided to check for output features.

bfd/

	* bfd/aoutx.h: Replace shared, executable, relocatable and pie
	fields with bfd_link_executable, bfd_link_dll,
	bfd_link_relocatable, bfd_link_pic and bfd_link_pie.
	* bfd/bout.c: Likewise.
	* bfd/coff-alpha.c: Likewise.
	* bfd/coff-arm.c: Likewise.
	* bfd/coff-i386.c: Likewise.
	* bfd/coff-i960.c: Likewise.
	* bfd/coff-m68k.c: Likewise.
	* bfd/coff-mcore.c: Likewise.
	* bfd/coff-mips.c: Likewise.
	* bfd/coff-ppc.c: Likewise.
	* bfd/coff-rs6000.c: Likewise.
	* bfd/coff-sh.c: Likewise.
	* bfd/coff-tic80.c: Likewise.
	* bfd/coff-x86_64.c: Likewise.
	* bfd/coff64-rs6000.c: Likewise.
	* bfd/coffgen.c: Likewise.
	* bfd/cofflink.c: Likewise.
	* bfd/ecoff.c: Likewise.
	* bfd/ecofflink.c: Likewise.
	* bfd/elf-bfd.h: Likewise.
	* bfd/elf-eh-frame.c: Likewise.
	* bfd/elf-ifunc.c: Likewise.
	* bfd/elf-m10200.c: Likewise.
	* bfd/elf-m10300.c: Likewise.
	* bfd/elf-s390-common.c: Likewise.
	* bfd/elf-vxworks.c: Likewise.
	* bfd/elf.c: Likewise.
	* bfd/elf32-arm.c: Likewise.
	* bfd/elf32-avr.c: Likewise.
	* bfd/elf32-bfin.c: Likewise.
	* bfd/elf32-cr16.c: Likewise.
	* bfd/elf32-cr16c.c: Likewise.
	* bfd/elf32-cris.c: Likewise.
	* bfd/elf32-crx.c: Likewise.
	* bfd/elf32-d10v.c: Likewise.
	* bfd/elf32-dlx.c: Likewise.
	* bfd/elf32-epiphany.c: Likewise.
	* bfd/elf32-fr30.c: Likewise.
	* bfd/elf32-frv.c: Likewise.
	* bfd/elf32-ft32.c: Likewise.
	* bfd/elf32-h8300.c: Likewise.
	* bfd/elf32-hppa.c: Likewise.
	* bfd/elf32-i370.c: Likewise.
	* bfd/elf32-i386.c: Likewise.
	* bfd/elf32-i860.c: Likewise.
	* bfd/elf32-ip2k.c: Likewise.
	* bfd/elf32-iq2000.c: Likewise.
	* bfd/elf32-lm32.c: Likewise.
	* bfd/elf32-m32c.c: Likewise.
	* bfd/elf32-m32r.c: Likewise.
	* bfd/elf32-m68hc11.c: Likewise.
	* bfd/elf32-m68hc1x.c: Likewise.
	* bfd/elf32-m68k.c: Likewise.
	* bfd/elf32-mcore.c: Likewise.
	* bfd/elf32-mep.c: Likewise.
	* bfd/elf32-metag.c: Likewise.
	* bfd/elf32-microblaze.c: Likewise.
	* bfd/elf32-moxie.c: Likewise.
	* bfd/elf32-msp430.c: Likewise.
	* bfd/elf32-mt.c: Likewise.
	* bfd/elf32-nds32.c: Likewise.
	* bfd/elf32-nios2.c: Likewise.
	* bfd/elf32-or1k.c: Likewise.
	* bfd/elf32-ppc.c: Likewise.
	* bfd/elf32-rl78.c: Likewise.
	* bfd/elf32-rx.c: Likewise.
	* bfd/elf32-s390.c: Likewise.
	* bfd/elf32-score.c: Likewise.
	* bfd/elf32-score7.c: Likewise.
	* bfd/elf32-sh-symbian.c: Likewise.
	* bfd/elf32-sh.c: Likewise.
	* bfd/elf32-sh64.c: Likewise.
	* bfd/elf32-spu.c: Likewise.
	* bfd/elf32-tic6x.c: Likewise.
	* bfd/elf32-tilepro.c: Likewise.
	* bfd/elf32-v850.c: Likewise.
	* bfd/elf32-vax.c: Likewise.
	* bfd/elf32-visium.c: Likewise.
	* bfd/elf32-xc16x.c: Likewise.
	* bfd/elf32-xstormy16.c: Likewise.
	* bfd/elf32-xtensa.c: Likewise.
	* bfd/elf64-alpha.c: Likewise.
	* bfd/elf64-hppa.c: Likewise.
	* bfd/elf64-ia64-vms.c: Likewise.
	* bfd/elf64-mmix.c: Likewise.
	* bfd/elf64-ppc.c: Likewise.
	* bfd/elf64-s390.c: Likewise.
	* bfd/elf64-sh64.c: Likewise.
	* bfd/elf64-x86-64.c: Likewise.
	* bfd/elflink.c: Likewise.
	* bfd/elfnn-aarch64.c: Likewise.
	* bfd/elfnn-ia64.c: Likewise.
	* bfd/elfxx-mips.c: Likewise.
	* bfd/elfxx-sparc.c: Likewise.
	* bfd/elfxx-tilegx.c: Likewise.
	* bfd/i386linux.c: Likewise.
	* bfd/linker.c: Likewise.
	* bfd/m68klinux.c: Likewise.
	* bfd/pdp11.c: Likewise.
	* bfd/pe-mips.c: Likewise.
	* bfd/peXXigen.c: Likewise.
	* bfd/reloc.c: Likewise.
	* bfd/reloc16.c: Likewise.
	* bfd/sparclinux.c: Likewise.
	* bfd/sunos.c: Likewise.
	* bfd/vms-alpha.c: Likewise.
	* bfd/xcofflink.c: Likewise.

include/

	* include/bfdlink.h (output_type): New enum.
	(bfd_link_executable): New macro.
	(bfd_link_dll): Likewise.
	(bfd_link_relocatable): Likewise.
	(bfd_link_pic): Likewise.
	(bfd_link_pie): Likewise.
	(bfd_link_info): Remove shared, executable, pie and relocatable.
	Add output_type and pic.

ld/

	* ld/ldctor.c: Replace shared, executable, relocatable and pie
	fields with bfd_link_executable, bfd_link_dll,
	bfd_link_relocatable, bfd_link_pic and bfd_link_pie.
	* ld/ldemul.c: Likewise.
	* ld/ldfile.c: Likewise.
	* ld/ldlang.c: Likewise.
	* ld/ldmain.c: Likewise.
	* ld/ldwrite.c: Likewise.
	* ld/lexsup.c: Likewise.
	* ld/pe-dll.c: Likewise.
	* ld/plugin.c: Likewise.
	* ld/emultempl/aarch64elf.em: Likewise.
	* ld/emultempl/aix.em: Likewise.
	* ld/emultempl/alphaelf.em: Likewise.
	* ld/emultempl/armcoff.em: Likewise.
	* ld/emultempl/armelf.em: Likewise.
	* ld/emultempl/avrelf.em: Likewise.
	* ld/emultempl/beos.em: Likewise.
	* ld/emultempl/cr16elf.em: Likewise.
	* ld/emultempl/elf-generic.em: Likewise.
	* ld/emultempl/elf32.em: Likewise.
	* ld/emultempl/genelf.em: Likewise.
	* ld/emultempl/generic.em: Likewise.
	* ld/emultempl/gld960.em: Likewise.
	* ld/emultempl/gld960c.em: Likewise.
	* ld/emultempl/hppaelf.em: Likewise.
	* ld/emultempl/irix.em: Likewise.
	* ld/emultempl/linux.em: Likewise.
	* ld/emultempl/lnk960.em: Likewise.
	* ld/emultempl/m68hc1xelf.em: Likewise.
	* ld/emultempl/m68kcoff.em: Likewise.
	* ld/emultempl/m68kelf.em: Likewise.
	* ld/emultempl/metagelf.em: Likewise.
	* ld/emultempl/mipself.em: Likewise.
	* ld/emultempl/mmo.em: Likewise.
	* ld/emultempl/msp430.em: Likewise.
	* ld/emultempl/nds32elf.em: Likewise.
	* ld/emultempl/needrelax.em: Likewise.
	* ld/emultempl/nios2elf.em: Likewise.
	* ld/emultempl/pe.em: Likewise.
	* ld/emultempl/pep.em: Likewise.
	* ld/emultempl/ppc32elf.em: Likewise.
	* ld/emultempl/ppc64elf.em: Likewise.
	* ld/emultempl/sh64elf.em: Likewise.
	* ld/emultempl/solaris2.em: Likewise.
	* ld/emultempl/spuelf.em: Likewise.
	* ld/emultempl/sunos.em: Likewise.
	* ld/emultempl/tic6xdsbt.em: Likewise.
	* ld/emultempl/ticoff.em: Likewise.
	* ld/emultempl/v850elf.em: Likewise.
	* ld/emultempl/vms.em: Likewise.
	* ld/emultempl/vxworks.em: Likewise.
2015-08-18 05:51:19 -07:00
Andrew Burgess 0a61824343 ld: Add '--require-defined' command line option.
Add a new command line option '--require-defined' to the linker.  This
option operates identically to the '--undefined' option, except that if
the symbol is not defined in the final output file then the linker will
exit with an error.

When making use of --gc-section, or just when trying to pull in parts of
a library, it is not uncommon for a user to use the '--undefined'
command line option to specify a symbol that the user then expects to be
defined by one of the object files supplied to the link.

However, if for any reason the symbol is not satisfied by an object
provided to the link the user will be left with an undefined symbol in
the output file, instead of a defined symbol.

In some cases the above behaviour is what the user wants, in other cases
though we can do better.  The '--require-defined' option tries to fill
this gap.  The symbol passed to the '--require-defined' option is
treated exactly as if the symbol was passed to '--undefined', however,
before the linker exits a check is made that all symbols passed to
'--require-defined' are actually defined, if any are not then the link
will fail with an error.

ld/ChangeLog:

	* ld.texinfo (Options): Document --require-defined option.
	* ldlang.c (struct require_defined_symbol): New structure.
	(require_defined_symbol_list): New variable.
	(ldlang_add_require_defined): New function.
	(ldlang_check_require_defined_symbols): New function.
	(lang_process): Check required symbols are defined.
	* ldlang.h (ldlang_add_require_defined): Declare.
	* ldlex.h (enum option_values): Add OPTION_REQUIRE_DEFINED_SYMBOL.
	* lexsup.c (ld_options): Add '--require-defined' entry.
	(parse_args): Handle '--require-defined' entry.
	* NEWS: Mention new '--require-defined' option.

ld/testsuite/ChangeLog:

	* ld-undefined/require-defined-1.d: New file.
	* ld-undefined/require-defined-2.d: New file.
	* ld-undefined/require-defined-3.d: New file.
	* ld-undefined/require-defined-4.d: New file.
	* ld-undefined/require-defined-5.d: New file.
	* ld-undefined/require-defined.exp: New file.
	* ld-undefined/require-defined.s: New file.
2015-08-04 10:00:05 +01:00
Tristan Gingold 3604cb1f8c Add new ld option: --print-memory-usage
ld/
	* NEWS: Mention new option.
	* lexsup.c (parse_args): Handle --print-memory-usage.
	(ld_options): Add --print-memory-usage.
	* ldmain.c (main): Call lang_print_memory_usage.
	* ldlex.h (enum option_values): Add OPTION_PRINT_MEMORY_USAGE.
	* ldlang.h: Add prototype of lang_print_memory_usage.
	* ldlang.c (lang_print_memory_size, lang_print_memory_usage): New
	functions.
	* ld.texinfo (Options): Document --print-memory-usage.
	* ld.h (args_type): Add print_memory_usage field.

ld/testsuite/
	* ld-scripts/print-memory-usage-1.t,
	* ld-scripts/print-memory-usage-1.s,
	* ld-scripts/print-memory-usage-1.l,
	* ld-scripts/print-memory-usage-2.t,
	* ld-scripts/print-memory-usage-2.l,
	* ld-scripts/print-memory-usage-3.s,
	* ld-scripts/print-memory-usage-3.t,
	* ld-scripts/print-memory-usage-3.l,
	* ld-scripts/print-memory-usage.t: New tests.
	* ld-scripts/print-memory-usage.exp: Run them.
2015-06-12 11:24:36 +02:00
Alan Modra 0e5fabeb2c Rewrite relro adjusting code
The linker tries to put the end of the last section in the relro
segment exactly on a page boundary, because the relro segment itself
must end on a page boundary.  If for any reason this can't be done,
padding is inserted.  Since the end of the relro segment is typically
between .got and .got.plt, padding effectively increases the size of
the GOT.  This isn't nice for targets and code models with limited GOT
addressing.

The problem with the current code is that it doesn't cope very well
with aligned sections in the relro segment.  When making .got aligned
to a 256 byte boundary for PowerPC64, I found that often the initial
alignment attempt failed and the fallback attempt to be less than
adequate.  This is a particular problem for PowerPC64 since the
distance between .got and .plt affects the size of plt call stubs,
leading to "stubs don't match calculated size" errors.

So this rewrite takes a direct approach to calculating a new relro
base.  Starting from the last section in the segment, we calculate
where it must start to position its end on the boundary, or as near as
possible considering alignment requirements.  The new start then
becomes the goal for the previous section to end, and so on for all
sections.  This of course ignores the possibility that user scripts
will place . = ALIGN(xxx); in the relro segment, or provide section
address expressions.  In those cases we might fail, but the old code
probably did too, and a fallback is provided.

ld/
	* ldexp.h (struct ldexp_control): Delete dataseg.min_base.  Add
	data_seg.relro_offset.
	* ldexp.c (fold_binary <DATA_SEGMENT_ALIGN>): Don't set min_base.
	(fold_binary <DATA_SEGMENT_RELRO_END>): Do set relro_offset.
	* ldlang.c (lang_size_sections): Rewrite code adjusting relro
	segment base to line up last section on page boundary.
ld/testsuite/
	* ld-x86-64/pr18176.d: Update.
2015-04-22 23:19:59 +09:30
Alan Modra 6166ed9330 Mark _init and _fini for --gc-sections
gold marks _init and _fini via symbol, ld marks them via section
(default scripts set .init and .fini section KEEP).  This makes it
possible for people to write their own _init and not bother to put the
function into the right section.

	PR ld/18223
	* ldlang.c (lang_process): Add _init and _fini to gc_sym_list.
2015-04-10 10:27:03 +09:30
Alan Modra 0fad6e4b94 Cast shift expression
PR ld/18176
	* ldlang.c (lang_size_sections): Cast shift expression.
2015-04-07 23:21:42 +09:30
Alan Modra 6c1aca3e2d Start of relro segment adjustment
Adjusting the start of the relro segment in order to make it end
exactly on a page boundary runs into difficulties when sections in the
relro segment are aligned;  Adjusting the start by (next_page - end)
sometimes results in more than that adjustment occurring at the end,
overrunning the page boundary.  So when that occurs we try a new lower
start position by masking the adjusted start with the maximum section
alignment.  However, we didn't consider that this masked start address
may in fact be before the initial relro base, which is silly since
that can only increase padding at the relro end.

I've also moved some calculations closer to where they are used, and
comments closer to the relevant statements.

	* ldlang.c (lang_size_sections): When alignment of sections
	results in relro base adjustment being too large, don't go lower
	than the initial value.
	* ldexp.c (fold_binary <DATA_SEGMENT_RELRO_END>): Comment.
	* scripttempl/elf.sc (DATA_SEGMENT_ALIGN): Omit SEGMENT_SIZE
	alignment when SEGMENT_SIZE is the same as MAXPAGESIZE.
2015-04-01 19:37:58 +10:30
Nick Clifton f0673d2040 Fixes a problem with the linker script parser not always handling the ! character in memort region attributes.
PR ld/17900
	* ldlang.c (lang_set_flags): Allow exclamation mark to reverse the
	sense of the following memory region attribute characters.
2015-02-27 16:19:57 +00:00
Alan Modra ce875075f9 PR ld/17973 LTO file syms
LTO output objects have an STT_FILE symbol using the name of the file,
a temporary file.  This results in executables that can't be exactly
reproduced, so the file name needs to be dropped.  We don't want to
lose all file symbols when linking a mix of lto and non-lto objects as
a file symbol can be used to figure which source file generated a
given local symbol.  So lto output objects need to be marked.

I chose to mark lto output objects with a new bfd flag.  This flag is
also used to fix a bug in the link-once handling;  An object being
loaded after "loading_lto_outputs" is set might be one extracted from
an archive to satisfy new references from lto objects, not an lto
object itself.

The new flag is copied from archive to elements, and the same done
for no_export.  This fixes a bug in that --exclude-libs doesn't work
with thin archives.  I'm not completely happy with this part of the
patch and may revist this to avoid the hack in
_bfd_look_for_bfd_in_cache.

	PR ld/17973
include/
	* bfdlink.h (struct bfd_link_info): Delete loading_lto_outputs.
bfd/
	* bfd.c (struct bfd): Add lto_output.
	* linker.c (_bfd_handle_already_linked): Explicitly test for
	objects added by the lto plugin.
	* opncls.c (_bfd_new_bfd_contained_in): Copy lto_output and
	no_export flags from archive.
	* archive.c (open_nested_file): New function, setting lto_output
	and no_export, extracted from..
	(find_nested_archive): ..here.  Flip params.  Rename from
	_bfd_find_nested_archive.
	(_bfd_get_elt_at_filepos): Correct var typo.  Use open_nested_file.
	(_bfd_look_for_bfd_in_cache): Copy no_export.
	* elflink.c (elf_link_add_object_symbols): Remove now unnecessary
	my_archive->no_export test.
	(elf_link_input_bfd): Drop existing lto_output STT_FILE syms.
	Don't use the file name when adding lto_output STT_FILE sym.
	* bfd-in2.h: Regenerate.
ld/
	* ldlang.h (struct lang_input_statement_flags): Add lto_output.
	* ldlang.c (lang_process): Don't set loading_lto_outputs.
	* ldfile.c (ldfile_try_open_bfd): Transfer entry flags.lto_output
	to bfd.
	* plugin.c (add_input_file, add_input_library): Set flags.lto_output.
2015-02-14 23:41:54 +10:30
H.J. Lu 716db898b7 Remove plugin_active_plugins_p()
Replace plugin_active_plugins_p() with link_info.lto_plugin_active.

	* ldfile.c (ldfile_try_open_bfd): Replace plugin_active_plugins_p()
	with link_info.lto_plugin_active.
	* ldlang.c (lang_process): Likewise.
	* ldmain.c (add_archive_element): Likewise.
	* plugin.c (plugin_active_plugins_p): Removed.
	* plugin.h (plugin_active_plugins_p): Likewise.
2015-01-29 04:56:39 -08:00
Alan Modra dbd1e97e32 PowerPC64 changes for xlc
The changes to reorder sections for better relro protection on powerpc64,
3e2b0f31, 23283c1b, and 5ad18f16, run into a problem with xlc.
xlc -qdatalocal puts global variables into .toc, which means that .toc
must be writable.  The simplest way to accomplish this is to edit the
linker script to remove .toc sections from .got on detecting xlc object
files.

bfd/
	* elf64-ppc.h (struct ppc64_elf_params): Add "object_in_toc".
	* elf64-ppc.c (ppc64_elf_add_symbol_hook): Assume that global symbols
	in .toc indicate xlc compiled code that might require a rw .toc.
ld/
	* emulparams/elf64ppc.sh (INITIAL_READWRITE_SECTIONS): Define.
	* emultempl/ppc64elf.em (params): Init new field.
	(ppc_after_open): New function.
	(LDEMUL_AFTER_OPEN): Define.
	* ldlang.c (lang_final): Whitespace fix.
ld/testsuite/
	* ld-powerpc/tocvar.d, * ld-powerpc/tocvar.s: New test.
	* ld-powerpc/tocnovar.d, * ld-powerpc/tocnovar.s: New test.
	* ld-powerpc/powerpc.exp: Run tocvar and tocnovar.
2015-01-28 18:30:54 +10:30
Senthil Kumar Selvaraj cc9ad334a7 Allow symbols in MEMORY region specification
This patch fixes PR 4643 by allowing symbols in the LENGTH and ORIGIN
fields of MEMORY regions.  Previously, only constants and constant
expressions are allowed.

For the AVR target, this helps define memory constraints more
accurately (per device), without having to create a ton of device
specific linker scripts.

ld/
	PR 4643
	* ldexp.c (fold_name): Fold LENGTH only after
	lang_first_phase_enum.
	* ldgram.y (memory_spec): Don't evaluate ORIGIN and LENGTH
	rightaway.
	* ldlang.h (struct memory_region_struct): Add origin_exp and
	length_exp fields.
	* ldlang.c (lang_do_memory_regions): New.
	(lang_memory_region_lookup): Initialize origin_exp and
	length_exp fields.
	(lang_process): Call lang_do_memory_regions.
ld/testsuite/
	* ld-scripts/memory.t: Define new symbol tred.
	* ld-scripts/memory_sym.t: New.
	* ld-scripts/script.exp: Perform MEMORY with symbols test, and
	conditionally check values of linker symbols.
2015-01-28 15:06:48 +10:30
Andrew Burgess c05b575a8d ld: Don't evaluate unneeded PROVIDE expressions.
When creating a linker mapfile (using -Map=MAPFILE), we previously would
always try to evaluate the expression from a PROVIDE statement.

However, this is not always safe, consider:

  PROVIDE (foo = 0x10);
  PROVIDE (bar = foo);

In this example, if neither 'foo' or 'bar' is needed, then while
generating the linker mapfile evaluating the expression for 'foo' is
harmless (just the value 0x10).  However, evaluating the expression for
'bar' requires the symbol 'foo', which is undefined.  This used to cause
a fatal error.

This patch changes the behaviour, so that when the destination of the
PROVIDE is not defined (that is the PROVIDE is not going to provide
anything) the expression is not evaluated, and instead a special string
is displayed to indicate that the linker is discarding the PROVIDE
statement.

This change not only fixes the spurious undefined symbol error, but also
means that a user can now tell if a PROVIDE statement has provided
anything by inspecting the linker mapfile, something that could not be
done before.

ld/ChangeLog:

	* ldlang.c (print_assignment): Only evaluate the expression for a
	PROVIDE'd assignment when the destination is being defined.
	Display a special message for PROVIDE'd symbols that are not being
	provided.

ld/testsuite/ChangeLog:

	* ld-scripts/provide-4.d: New file.
	* ld-scripts/provide-4-map.d: New file.
	* ld-scripts/provide-4.t: New file.
	* ld-scripts/provide-5.d: New file.
	* ld-scripts/provide-5.s: New file.
	* ld-scripts/provide-5-map.d: New file.
	* ld-scripts/provide-5.t: New file.
	* ld-scripts/provide.exp: Run the provide-4.d and provide-5.d
	tests.
2015-01-20 09:49:27 +00:00
Alan Modra c4621b339e Fix garbage collection of common symbols
Running lang_common before garbage collection means slightly less work
in garbage collection code, since common symbols should no longer
appear there.  It does have the side effect of keeping linker script
symbols (at least those defined outside of sections) global too,
hence some testsuite churn.

bfd/
	PR 17165
	* elf-bfd.h (ELF_COMMON_DEF): Note that this might be true for
	linker script assignments too.
	* elflink.c (elf_gc_sweep_symbol): Don't drop ELF_COMMON_DEF syms.
	(bfd_elf_gc_mark_dynamic_ref_symbol): Similarly.
ld/
	PR 17165
	* ldlang.c (lang_process): Run lang_common before lang_gc_sections.
ld/testsuite/
	* ld-gc/pr14265.d,
	* ld-cris/tls-gc-68.d,
	* ld-cris/tls-gc-69.d,
	* ld-cris/tls-gc-70.d,
	* ld-cris/tls-gc-71.d,
	* ld-cris/tls-gc-75.d,
	* ld-cris/tls-gc-76.d,
	* ld-cris/tls-gc-79.d,
	* ld-mmix/bpo-10.d,
	* ld-mmix/bpo-11.d: Update.
2015-01-19 21:26:30 +10:30
Alan Modra b90efa5b79 ChangeLog rotatation and copyright year update 2015-01-02 00:53:45 +10:30
Alan Modra 18d6a79d35 Move support code for linker script DEFINED to ldexp.c
This moves support code for DEFINED to ldexp.c where it is used,
losing the lang_ prefix on identifiers.  Two new functions are needed
to initialize and clean up to hash table, but other than that there
are no functional changes here.

	* ldexp.c (struct definedness_hash_entry, definedness_table)
	(definedness_newfunc, symbol_defined, update_definedness): Move
	and rename from..
	* ldlang.h (struct lang_definedness_hash_entry): ..here,..
	* ldlang.c (lang_definedness_table, lang_definedness_newfunc)
	(lang_symbol_defined, lang_update_definedness): ..and here.
	* ldexp.c (ldexp_init, ldexp_finish): New functions, extracted from..
	* ldlang.c (lang_init, lang_finish): ..here.
	* ldexp.h (ldexp_init, ldexp_finish): Declare.
	* ldlang.h (lang_symbol_defined, lang_update_definedness): Delete.
	* ldmain.c (main): Call ldexp_init and ldexp_finish.
2014-12-23 23:36:50 +10:30