Commit Graph

4806 Commits

Author SHA1 Message Date
Jan Beulich
87fa7d568d bfd: don't silently wrap or truncate PE image section RVAs
In PE images section addresses get expressed as addresses relative to
the image base. Therefore the VA of a section must be no less than the
image base, and after subtraction of the image base the resulting value
should fit in 32 bits. (The issue is particularly obvious to notice when
sections, perhaps because of ELF assumptions, get placed at VA 0 by
default. Debugging info sections as well as .comment, when input files
are ELF, are a good example. All such sections need proper mentioning in
the linker script to avoid this warning.)

There are a number of test cases which previously produced bogus images,
yet still declared the test a success. Like done for other tests
already, force a zero image base for these. This then also allows (and
requires) dropping again xfail-s which 39a7b38fac ("Fix linker tests
to work with 16-bit targets") had added to ld-scripts/default-script*.d
(originally as skip-s). This also depends on similar adjustments to
testsuite/ld-scripts/map-address.* made by an earlier patch.

For ld-scripts/print-memory-usage.* I suppose xcoff could be dropped
from the exclusion list by suppressing garbage collection, just like
already done in e.g. (as seen in the diff here) ld-scripts/data.*, but I
didn't want to make unrelated adjustments.
2021-03-09 08:52:32 +01:00
H.J. Lu
8c0546e928 elf/x86-64: Subtract __ImageBase for R_AMD64_IMAGEBASE
When linking Windows x86-64 relocatable object files to generate x86-64
ELF executable, we need to subtract __ImageBase, aka __executable_start,
for R_AMD64_IMAGEBASE relocation:

1. Add link_info to struct output_elf_obj_tdata to store linker info and
_bfd_get_link_info() to retrieve it.
2. Add ldelf_set_output_arch to set up link_info.
3. Add pex64_link_add_symbols to create an indirect reference to
__executable_start for __ImageBase to support R_AMD64_IMAGEBASE relocation
when adding symbols from Windows x86-64 relocatable object files to
generate x86-64 ELF executable.
4. Also subtract __ImageBase for R_AMD64_IMAGEBASE when generating x86-64
ELF executable.

bfd/

	PR ld/27425
	PR ld/27432
	* bfd.c (_bfd_get_link_info): New function.
	* elf-bfd.h (output_elf_obj_tdata): Add link_info.
	(elf_link_info): New.
	* libbfd-in.h (_bfd_get_link_info): New prototype.
	* coff-x86_64.c (coff_amd64_reloc): Also subtract __ImageBase for
	R_AMD64_IMAGEBASE when generating x86-64 ELF executable.
	* pe-x86_64.c: Include "coff/internal.h" and "libcoff.h".
	(pex64_link_add_symbols): New function.
	(coff_bfd_link_add_symbols): New macro.
	* libbfd.h: Regenerated.

ld/

	PR ld/27425
	PR ld/27432
	* ldelf.c (ldelf_set_output_arch): New function.
	* ldelf.h (ldelf_set_output_arch): New prototype.
	* emultempl/elf.em (LDEMUL_SET_OUTPUT_ARCH): Default to
	ldelf_set_output_arch.
	* ld-x86-64/pe-x86-64-1.od: Expect __executable_start.
	* testsuite/ld-x86-64/pe-x86-64-2.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-3.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-4.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-5.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-6.obj.bz2: New file.
	* testsuite/ld-x86-64/pe-x86-64-6.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64.exp: Run ld/27425 test.
2021-03-05 18:25:06 -08:00
Jan Beulich
d4e5db4e50 ld: adjust ld-scripts/map-address.*
Without setting an image base address and without naming at least .text,
this test produces entirely bogus PE output. To be honest, even the ELF
output looks odd: .text gets placed at 0x10204, and both foo and bar get
associated with .text despite living below its start address.

Since neither image base nor .text placement are the subject of this
test, specify .text placement explicitly and in the PE case force the
image base to zero.
2021-03-04 16:56:40 +01:00
Jan Beulich
6fa7408d72 ld: don't generate base relocations in PE output for absolute symbols
It is the very nature of absolute symbols that they don't change even
if the loader decides to put the image at other than its link-time base
address. Of the linker-defined (and PE-specific) symbols __image_base__
(and its alias) needs special casing, as it'll still appear to be
absolute at this point.

A new inquiry function in ldexp.c is needed because PE base relocations
get generated before ldexp_finalize_syms() runs, yet whether a
relocation is needed depends on the ultimate property of a symbol.
2021-03-04 16:55:01 +01:00
Alan Modra
5789f845fb --gc-sections with groups and start/stop syms
The testcases added here show situations where synthesized start/stop
symbols don't cause their associated input sections to be marked.
Fixed with the elflink.c and ldlang.c changes.

bfd/
	PR 27500
	* elflink.c (_bfd_elf_gc_mark_rsec): Do special start/stop
	processing not when start/stop symbol section is unmarked but
	on first time a start/stop symbol is processed.
ld/
	* ldlang.c (insert_undefined): Don't mark symbols here.
	(lang_mark_undefineds): Do so here instead, new function.
	(lang_process): Call lang_mark_undefineds.
	* testsuite/ld-gc/start3.d,
	* testsuite/ld-gc/start3.s: New test.
	* testsuite/ld-gc/start4.d,
	* testsuite/ld-gc/start4.s: New test.
	* testsuite/ld-gc/gc.exp: Run them.
2021-03-03 17:46:36 +10:30
Alan Modra
270f32fc50 ld-gc tests on underscore targets
Adjust tests to reference __start and __stop syms with an extra
leading underscore when appropriate, and run tests on more targets.

	* testsuite/ld-gc/gc.exp: Define UNDERSCORE in ASFLAGS.
	Move tests with ELF section directives to is_elf_format block.
	* testsuite/ld-gc/abi-note.d: Run on more targets.
	* testsuite/ld-gc/pr19167.d: Likewise and adjust xfails.
	* testsuite/ld-gc/start.d: Likewise.
	* testsuite/ld-gc/start2.d: Likewise.
	* testsuite/ld-gc/stop.d: Likewise.
	* testsuite/ld-gc/pr19167a.s: Add support for underscore targets.
	* testsuite/ld-gc/start.s: Likewise.
	* testsuite/ld-gc/start2.s: Likewise.
2021-03-03 17:46:36 +10:30
Nick Alcock
211bcd0133 bfd, ld, libctf: skip zero-refcount strings in CTF string reporting
This is a tricky one.  BFD, on the linker's behalf, reports symbols to
libctf via the ctf_new_symbol and ctf_new_dynsym callbacks, which
ultimately call ctf_link_add_linker_symbol.  But while this happens
after strtab offsets are finalized, it happens before the .dynstr is
actually laid out, so we can't iterate over it at this stage and
it is not clear what the reported symbols are actually called.  So
a second callback, examine_strtab, is called after the .dynstr is
finalized, which calls ctf_link_add_strtab and ultimately leads
to ldelf_ctf_strtab_iter_cb being called back repeatedly until the
offsets of every string in the .dynstr is passed to libctf.

libctf can then use this to get symbol names out of the input (which
usually stores symbol types in the form of a name -> type mapping at
this stage) and extract the types of those symbols, feeding them back
into their final form as a 1:1 association with the real symtab's
STT_OBJ and STT_FUNC symbols (with a few skipped, see
ctf_symtab_skippable).

This representation is compact, but has one problem: if libctf somehow
gets confused about the st_type of a symbol, it'll stick an entry into
the function symtypetab when it should put it into the object
symtypetab, or vice versa, and *every symbol from that one on* will have
the wrong CTF type because it's actually looking up the type for a
different symbol.

And we have just such a bug.  ctf_link_add_strtab was not taking the
refcounts of strings into consideration, so even strings that had been
eliminated from the strtab by virtue of being in objects eliminated via
--as-needed etc were being reported.  This is harmful because it can
lead to multiple strings with the same apparent offset, and if the last
duplicate to be reported relates to an eliminated symbol, we look up the
wrong symbol from the input and gets its type wrong: if it's unlucky and
the eliminated symbol is also of the wrong st_type, we will end up with
a corrupted symtypetab.

Thankfully the wrong-st_type case is already diagnosed by a
this-can-never-happen paranoid warning:

  CTF warning: Symbol 61a added to CTF as a function but is of type 1

or the converse

 * CTF warning: Symbol a3 added to CTF as a data object but is of type 2

so at least we can tell when the corruption has spread to more than one
symbol's type.

Skipping zero-refcounted strings is easy: teach _bfd_elf_strtab_str to
skip them, and ldelf_ctf_strtab_iter_cb to loop over skipped strings
until it falls off the end or finds one that isn't skipped.

bfd/ChangeLog
2021-03-02  Nick Alcock  <nick.alcock@oracle.com>

	* elf-strtab.c (_bfd_elf_strtab_str): Skip strings with zero refcount.

ld/ChangeLog
2021-03-02  Nick Alcock  <nick.alcock@oracle.com>

	* ldelfgen.c (ldelf_ctf_strtab_iter_cb): Skip zero-refcount strings.

libctf/ChangeLog
2021-03-02  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-create.c (symtypetab_density): Report the symbol name as
	well as index in the name != object error; note the likely
	consequences.
	* ctf-link.c (ctf_link_shuffle_syms): Report the symbol index
	as well as name.
2021-03-02 15:10:10 +00:00
Alan Modra
b80e421f91 PR27451, -z start_stop_gc for powerpc64
PowerPC64 has its own gc_mark_dynamic_ref.

bfd/
	PR 27451
	* elf64-ppc.c (ppc64_elf_gc_mark_dynamic_ref): Ignore synthesized
	linker defined start/stop symbols when start_stop_gc.
ld/
	* testsuite/ld-powerpc/startstop.d,
	* testsuite/ld-powerpc/startstop.r,
	* testsuite/ld-powerpc/startstop.s: New test.
	* testsuite/ld-powerpc/powerpc.exp: Run it.
2021-03-02 21:49:56 +10:30
Alan Modra
f5b9c288a3 PowerPC64 undefined weak visibility vs GOT optimisation
Undefined weak symbols with non-default visibility are seen as local
by SYMBOL_REFERENCES_LOCAL.  This stops a got indirect to relative
optimisation for them, so that pies and dlls don't get non-zero values
when loading somewhere other than the address they are linked at
(which always happens).  The optimisation could be allowed for pdes,
but I thought it best not to allow it there too.

bfd/
	* elf64-ppc.c (ppc64_elf_relocate_section): Don't optimise got
	indirect to pc-relative or toc-relative for undefined symbols.
ld/
	* testsuite/ld-powerpc/weak1.d,
	* testsuite/ld-powerpc/weak1.r,
	* testsuite/ld-powerpc/weak1.s,
	* testsuite/ld-powerpc/weak1so.d,
	* testsuite/ld-powerpc/weak1so.r: New tests.
	* testsuite/ld-powerpc/powerpc.exp: Run them.
2021-03-02 21:47:42 +10:30
Nick Clifton
ba6eb62ff0 Add DWARF-5 section names to PE and PEP linker scripts.
PR 27268
	* scripttempl/pe.sc: Add DWARF-5 section names.
	* scripttempl/pep.sc: Likewise.
2021-03-01 16:25:06 +00:00
Alan Modra
8ee10e8609 PR27451, -z start_stop_gc
When --gc-sections is in effect, a reference from a retained section
to __start_SECNAME or __stop_SECNAME causes all input sections named
SECNAME to also be retained, if SECNAME is representable as a C
identifier and either __start_SECNAME or __stop_SECNAME is synthesized
by the linker.  Add an option to disable that feature, effectively
ignoring any relocation that references a synthesized linker defined
__start_ or __stop_ symbol.

	PR 27451
include/
	* bfdlink.h (struct bfd_link_info): Add start_stop_gc.
bfd/
	* elflink.c (_bfd_elf_gc_mark_rsec): Ignore synthesized linker
	defined start/stop symbols when start_stop_gc.
	(bfd_elf_gc_mark_dynamic_ref_symbol): Likewise.
	(bfd_elf_define_start_stop): Don't modify ldscript_def syms.
	* linker.c (bfd_generic_define_start_stop): Likewise.
ld/
	* emultempl/elf.em: Handle -z start-stop-gc and -z nostart-stop-gc.
	* lexsup.c (elf_static_list_options): Display help for them.  Move
	help for -z stack-size to here from elf_shlib_list_options. Add
	help for -z start-stop-visibility and -z undefs.
	* ld.texi: Document -z start-stop-gc and -z nostart-stop-gc.
	* NEWS: Mention -z start-stop-gc.
	* testsuite/ld-gc/start2.s,
	* testsuite/ld-gc/start2.d: New test.
	* testsuite/ld-gc/gc.exp: Run it.
2021-03-01 17:28:03 +10:30
Alan Modra
7824c1d22f Weak references to __start_/__stop_ symbols
If a weak reference to a __start_foo or __stop_foo symbol ends up
having no definition due to all the foo sections being removed for
some reason, undef_start_stop currently makes the symbol strong
undefined.  That risks a linker undefined symbol error.  Fix that by
making the symbol undefweak and also undo some dynamic symbol state.

Note that saving the state of the symbol type at the time
lang_init_start_stop runs is not sufficient.  The linker may have
merged in a shared library reference by that point and made what was
an undefweak in regular objects, a strong undefined.  So it is
necessary to look at the ELF symbol flags to decide whether an
undefweak is the proper resolution.

Something probably should be done for COFF/PE too, but I'm unsure how
to do go about that.

	* ldlang.c (undef_start_stop): For ELF make undefined start/stop
	symbols undefweak if that was how they were referenced.  Undo
	dynamic state too.
2021-03-01 14:26:39 +10:30
Alan Modra
6a1224ec76 PR27128, nm -P portable output format regression
Add nm --without-symbol-versions.

binutils/
	PR 27128
	* doc/binutils.texi: Add nm --with-symbol-versions and
	--without-symbol-versions documentation.
	* nm.c (with_symbol_versions): New variable.
	(enum long_option_values): Delete OPTION_WITH_SYMBOL_VERSIONS.
	(long_options): Make --with-symbol-versions entry twiddle the flag.
	Add --without-symbol-versions.
	(print_symname): Strip version when !with_symbol_versions.  Add
	dynamic version info under control of with_symbol_versions.
	(main): Remove OPTION_WITH_SYMBOL_VERSIONS case.
ld/
	* testsuite/ld-elf/pr25708.d: Add --with-symbol-versions to nm.
	* testsuite/ld-elf/pr27128a.d: Likewise.
	* testsuite/ld-elf/pr27128b.d: Likewise.
	* testsuite/ld-elf/pr27128c.d: Likewise.
	* testsuite/ld-elf/pr27128d.d: Likewise.
	* testsuite/ld-elf/pr27128e.d: Likewise.
2021-03-01 14:26:39 +10:30
Alan Modra
bfece7562d Add PR27441 testcase
PR 27441
	* testsuite/ld-plugin/pr27441a.c,
	* testsuite/ld-plugin/pr27441b.c,
	* testsuite/ld-plugin/pr27441c.c,
	* testsuite/ld-plugin/pr27441c.d: New test.
	* testsuite/ld-plugin/lto.exp: Run it.
2021-02-26 17:49:04 +10:30
Jan Beulich
3c27360bc4 ld: correct description of behavior for symbols redefined by script
Prior to 89753bbf81 ("Warn when a script redefines a symbol") there
was no diagnostic at all. As of that commit, it's a warning, not an
error.
2021-02-25 14:35:29 +01:00
Alan Modra
89753bbf81 Warn when a script redefines a symbol
Note that we don't even warn if scripts adjust a symbol as in
ld-elf/var1 and ld-scripts/pr14962.

include/
	* bfdlink.h (struct bfd_link_info): Add warn_multiple_definition.
ld/
	* ldexp.c (exp_fold_tree_1): Warn on script defining a symbol
	defined in an object file.
	* ldmain.c (multiple_definition): Heed info->warn_multiple_definition.
	* testsuite/ld-scripts/defined5.d: Expect a warning.
2021-02-21 14:28:16 +10:30
Alan Modra
2f973f134d Wrong ELF class plugin vs. gcc ld version
When building 32-bit binutils with CC="gcc -m32" CXX="g++ -m32" we can
fail the gcc ld version test due to an error attempting to load a
64-bit plugin into 32-bit ld-new.  This results in bogus errors about
"Your compiler driver ignores -B when choosing ld."

	* testsuite/lib/ld-lib.exp: Whitespace.
	(load_common_lib): Expand single use and delete this proc.
	(run_host_cmd): Use -fno-lto when getting gcc's ld version.
	Use -B for clang too.
2021-02-19 13:49:15 +10:30
Andrew Burgess
a364a116f9 ld: remove stray debug fprintf
In this commit:

  commit ace667e59a
  Date:   Mon Jul 18 21:00:00 2016 +0100

      ld: Restore file offset after a plugin fails to claim a file

I inadvertently left in a stray fprintf call.  Removed in this commit.

ld/ChangeLog:

	* testplugin.c (record_read_length): Remove debug fprintf.
2021-02-18 10:36:39 +00:00
Alan Modra
5b1f6c9570 ld testsuite: change unresolved to unsupported/fail
"unresolved" as a test result means runtest returns an error, which
can be confusing when there is no apparent error unless you look in
.log files.  In particular many tests are skipped without reporting an
error if no target C compiler is found, but if a target C compiler is
found but won't compile a testcase for some reason we used to mark the
test as unresolved.  Which is no more worthy of an error than when
lacking a C compiler entirely.

	* testsuite/ld-cdtest/cdtest.exp,
	* testsuite/ld-checks/checks.exp,
	* testsuite/ld-elf/binutils.exp,
	* testsuite/ld-elf/compress.exp,
	* testsuite/ld-elf/dwarf.exp,
	* testsuite/ld-elf/exclude.exp,
	* testsuite/ld-elf/frame.exp,
	* testsuite/ld-elf/indirect.exp,
	* testsuite/ld-elf/linux-x86.exp,
	* testsuite/ld-elf/sec-to-seg.exp,
	* testsuite/ld-elf/tls_common.exp,
	* testsuite/ld-elfcomm/elfcomm.exp,
	* testsuite/ld-elfvers/vers.exp,
	* testsuite/ld-elfvsb/elfvsb.exp,
	* testsuite/ld-elfweak/elfweak.exp,
	* testsuite/ld-ifunc/binutils.exp,
	* testsuite/ld-mips-elf/mips-elf-flags.exp,
	* testsuite/ld-misc/defsym.exp,
	* testsuite/ld-mn10300/mn10300.exp,
	* testsuite/ld-plugin/lto.exp,
	* testsuite/ld-plugin/plugin.exp,
	* testsuite/ld-scripts/align.exp,
	* testsuite/ld-scripts/assert.exp,
	* testsuite/ld-scripts/crossref.exp,
	* testsuite/ld-scripts/defined.exp,
	* testsuite/ld-scripts/extern.exp,
	* testsuite/ld-scripts/log2.exp,
	* testsuite/ld-scripts/map-address.exp,
	* testsuite/ld-scripts/phdrs.exp,
	* testsuite/ld-scripts/phdrs2.exp,
	* testsuite/ld-scripts/script.exp,
	* testsuite/ld-scripts/section-flags.exp,
	* testsuite/ld-scripts/sizeof.exp,
	* testsuite/ld-scripts/weak.exp,
	* testsuite/ld-selective/selective.exp,
	* testsuite/ld-sh/sh.exp,
	* testsuite/ld-shared/shared.exp,
	* testsuite/ld-srec/srec.exp,
	* testsuite/ld-tic6x/tic6x.exp,
	* testsuite/ld-undefined/undefined.exp,
	* testsuite/ld-undefined/weak-undef.exp,
	* testsuite/lib/ld-lib.exp: Don't use unresolved except after
	perror.  Instead report "unsupported" or "fail".
2021-02-14 22:34:27 +10:30
Nick Clifton
c46b706620 Change the readelf and objdump programs so that they will automatically follow links to separate debug info files.
* configure.ac (follow-debug-links): Add option to enable or
	disable the following of debug links by default.  Set the
	default for the option to be 'follow'.
	* dwarf.c (do_follow_links): Initialise with DEFAULT_FOR_FOLLOW_LINKS.
	(dwarf_select_sections_by_names): Add no-follow-links option.
	(dwarf_select_sections_by_letter): Add 'N' option.
	* objdump.c (usage): Add conditional text describing the
	follow links option.
	(slurp_symtab): Ensure that there is a NULL entry at the end
	of the symbol table.
	(slurp_dynamic_symtab): Likewise.
	(dump_bfd): When extending the symbol table, ensure that there
	is still a NULL entry at the end.
	* readelf.c (usage): Add conditional text describing the
	follow links option.
	* doc/binutils.texi: Update documentation for objcopy and
	readelf.
	* doc/debug.options.texi: Update documentation of the
	follow-links option.
	* config.in: Regenerate.
	* configure: Regenerate.
	* testsuite/binutils-all/compress.exp: Add the -WN option to
	objdump command lines that are not expecting to follow links.
	* testsuite/binutils-all/readelf.exp: Add the
	--debug-dump=no-follow-links option to tests that are not
	expecting to follow debug links.

gas	* testsuite/gas/mach-o/sections-1.d: Stop automatic debug link
        following.
	* testsuite/gas/xgate/insns-dwarf2.d: Likewise.

ld	* testsuite/ld-elf/sec64k.exp: Stop readelf from automatically
	following debug links.
2021-02-12 14:52:22 +00:00
Alan Modra
933feaf37e Re: Remove arm-symbianelf
gas/
	* NEWS: Mention arm-symbianelf removal.
ld/
	* NEWS: Mention arm-symbianelf removal.
2021-02-11 07:22:20 +10:30
Alan Modra
a57d17732e Remove arm-symbianelf
* configure.ac: Delete arm*-*-symbianelf* entry.
	* configure: Regenerate.
bfd/
	* config.bfd (arm*-*-symbianelf*): Move from obsolete to removed.
	* configure.ac: Delete symbian entries.
	* elf-bfd.h (enum elf_target_os): Delete is_symbian.
	* elf32-arm.c: Remove symbian support.  Formatting.
	* targets.c: Delete symbian entries.
	* configure: Regenerate.
binutils/
	* testsuite/lib/binutils-common.exp (supports_gnu_osabi): Remove
	symbianelf.
gas/
	* Makefile.am (TARG_ENV_HFILES): Remove config/te-symbian.h.
	* config/tc-arm.c (elf32_arm_target_format): Remove TE_SYMBIAN
	support.
	* config/te-symbian.h: Delete.
	* configure.tgt: Remove arm-*-symbianelf*.
	* testsuite/gas/arm/arch4t-eabi.d: Don't mention symbianelf in
	target selection.
	* testsuite/gas/arm/arch4t.d: Likewise.
	* testsuite/gas/arm/got_prel.d: Likewise.
	* testsuite/gas/arm/mapdir.d: Likewise.
	* testsuite/gas/arm/mapmisc.d: Likewise.
	* testsuite/gas/arm/mapsecs.d: Likewise.
	* testsuite/gas/arm/mapshort-eabi.d: Likewise.
	* testsuite/gas/arm/thumb-eabi.d: Likewise.
	* testsuite/gas/arm/thumb.d: Likewise.
	* testsuite/gas/arm/thumbrel.d: Likewise.
	* Makefile.in: Regenerate.
	* po/POTFILES.in: Regenerate.
ld/
	* Makefile.am (ALL_EMULATION_SOURCES): Remove earmsymbian.c.
	Don't include symbian dep file.
	* configure.tgt: Remove arm*-*-symbianelf* entry.
	* emulparams/armsymbian.sh: Delete.
	* ld.texi: Don't mention symbian.
	* scripttempl/armbpabi.sc: Delete.
	* testsuite/ld-arm/symbian-seg1.d: Delete.
	* testsuite/ld-arm/symbian-seg1.s: Delete.
	* testsuite/ld-arm/arm-elf.exp: Don't run symbian-seg1.
	* Makefile.in: Regenerate.
	* po/BLD-POTFILES.in: Regenerate.
2021-02-09 23:36:16 +10:30
H.J. Lu
38a143aa8c ld: Remove x86 ISA level run-time tests
Remove x86 ISA level run-time tests since with glibc 2.33, they will fail
to run on machines with lesser x86 ISA level:

tmpdir/property-5-pie: CPU ISA level is lower than required

	PR ld/27358
	* testsuite/ld-i386/i386.exp: Remove property 3/4/5 run-time
	tests.
	* testsuite/ld-x86-64/x86-64.exp: Likewise.
2021-02-07 13:12:42 -08:00
Nick Alcock
35a01a0454 libctf, ld: fix symtypetab and var section population under ld -r
The variable section in a CTF dict is meant to contain the types of
variables that do not appear in the symbol table (mostly file-scope
static declarations).  We implement this by having the compiler emit
all potential data symbols into both sections, then delete those
symbols from the variable section that correspond to data symbols the
linker has reported.

Unfortunately, the check for this in ctf_serialize is wrong: rather than
checking the set of linker-reported symbols, we check the set of names
in the data object symtypetab section: if the linker has reported no
symbols at all (usually if ld -r has been run, or if a non-linker
program that does not use symbol tables is calling ctf_link) this will
include every single symbol, emptying the variable section completely.

Worse, when ld -r is in use, we want to force writeout of every
symtypetab entry on the inputs, in an indexed section, whether or not
the linker has reported them, since this isn't a final link yet and the
symbol table is not finalized (and may grow more symbols than the linker
has yet reported).  But the check for this is flawed too: we were
relying on ctf_link_shuffle_syms not having been called if no symbols
exist, but that function is *always* called by ld even when ld -r is in
use: ctf_link_add_linker_symbol is the one that's not called when there
are no symbols.

We clearly need to rethink this.  Using the emptiness of the set of
reported symbols as a test for ld -r is just ugly: the linker already
knows if ld -r is underway and can just tell us.  So add a new linker
flag CTF_LINK_NO_FILTER_REPORTED_SYMS that is set to stop the linker
filtering the symbols in the symtypetab sections using the set that the
linker has reported: use the presence or absence of this flag to
determine whether to emit unindexed symtabs: we only remove entries from
the variable section when filtering symbols, and we only remove them if
they are in the reported symbol set, fixing the case where no symbols
are reported by the linker at all.

(The negative sense of the new CTF_LINK flag is intentional: the common
case, both for ld and for simple tools that want to do a ctf_link with
no ELF symbol table in sight, is probably to filter out symbols that no
linker has reported: i.e., for the simple tools, all of them.)

There's another wrinkle, though.  It is quite possible for a non-linker
to add symbols to a dict via ctf_add_*_sym and then write it out via the
ctf_write APIs: perhaps it's preparing a dict for a later linker
invocation.  Right now this would not lead to anything terribly
meaningful happening: ctf_serialize just assumes it was called via
ctf_link if symbols are present.  So add an (internal-to-libctf) flag
that indicates that a writeout is happening via ctf_link_write, and set
it there (propagating it to child dicts as needed).  ctf_serialize can
then spot when it is not being called by a linker, and arrange to always
write out an indexed, sorted symtypetab for fastest possible future
symbol lookup by name in that case.  (The writeouts done by ld -r are
unsorted, because the only thing likely to use those symtabs is the
linker, which doesn't benefit from symtypetab sorting.)

Tests added for all three linking cases (ld -r, ld -shared, ld), with a
bit of testsuite framework enhancement to stop it unconditionally
linking the CTF to be checked by the lookup program with -shared, so
tests can now examine CTF linked with -r or indeed with no flags at all,
though the output filename is still foo.so even in this case.

Another test added for the non-linker case that endeavours to determine
whether the symtypetab is sorted by examining the order of entries
returned from ctf_symbol_next: nobody outside libctf should rely on
this ordering, but this test is not outside libctf :)

include/ChangeLog
2021-01-26  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-api.h (CTF_LINK_NO_FILTER_REPORTED_SYMS): New.

ld/ChangeLog
2021-01-26  Nick Alcock  <nick.alcock@oracle.com>

	* ldlang.c (lang_merge_ctf): Set CTF_LINK_NO_FILTER_REPORTED_SYMS
	when appropriate.

libctf/ChangeLog
2021-01-27  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-impl.c (_libctf_nonnull_): Add parameters.
	(LCTF_LINKING): New flag.
	(ctf_dict_t) <ctf_link_flags>: Mention it.
	* ctf-link.c (ctf_link): Keep LCTF_LINKING set across call.
	(ctf_write): Likewise, including in child dictionaries.
	(ctf_link_shuffle_syms): Make sure ctf_dynsyms is NULL if there
	are no reported symbols.
	* ctf-create.c (symtypetab_delete_nonstatic_vars): Make sure
	the variable has been reported as a symbol by the linker.
	(symtypetab_skippable): Mention relationship between SYMFP and the
	flags.
	(symtypetab_density): Adjust nonnullity.  Exit early if no symbols
	were reported and force-indexing is off (i.e., we are doing a
	final link).
	(ctf_serialize): Handle the !LCTF_LINKING case by writing out an
	indexed, sorted symtypetab (and allow SYMFP to be NULL in this
	case).  Turn sorting off if this is a non-final link.  Only delete
	nonstatic vars if we are filtering symbols and the linker has
	reported some.
	* testsuite/libctf-regression/nonstatic-var-section-ld-r*:
	New test of variable and symtypetab section population when
	ld -r is used.
	* testsuite/libctf-regression/nonstatic-var-section-ld-executable.lk:
	Likewise, when ld of an executable is used.
	* testsuite/libctf-regression/nonstatic-var-section-ld.lk:
	Likewise, when ld -shared alone is used.
	* testsuite/libctf-regression/nonstatic-var-section-ld*.c:
	Lookup programs for the above.
	* testsuite/libctf-writable/symtypetab-nonlinker-writeout.*: New
	test, testing survival of symbols across ctf_write paths.
	* testsuite/lib/ctf-lib.exp (run_lookup_test): New option,
	nonshared, suppressing linking of the SOURCE with -shared.
2021-02-04 16:01:53 +00:00
H.J. Lu
1a2f1b54a5 x86-64: Provide more info when failed to convert GOTPCREL
Provide

[hjl@gnu-cfl-2 ld]$ ./ld-new -z norelro  -L/export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64  -melf32_x86_64 -Ttext=0x80000000 -o tmpdir/dump tmpdir/pr19609-7.o
tmpdir/pr19609-7.o: in function `_start':
(.text+0x2): failed to convert GOTPCREL relocation against 'foobar'; relink with --no-relax
[hjl@gnu-cfl-2 ld]$

instead of

[hjl@gnu-cfl-2 ld]$ ld -z norelro  -L/export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64  -melf32_x86_64 -Ttext=0x80000000 -o tmpdir/dump tmpdir/pr19609-7.o
ld: failed to convert GOTPCREL relocation; relink with --no-relax
[hjl@gnu-cfl-2 ld]$

bfd/

	PR ld/19609
	* elf64-x86-64.c (elf_x86_64_relocate_section): Provide more
	info when failed to convert GOTPCREL relocation.

ld/

	PR ld/19609
	* testsuite/ld-x86-64/pr19609-2a.d: Updated.
	* testsuite/ld-x86-64/pr19609-2b.d: Likewise.
	* testsuite/ld-x86-64/pr19609-4a.d: Likewise.
	* testsuite/ld-x86-64/pr19609-4c.d: Likewise.
	* testsuite/ld-x86-64/pr19609-5d.d: Likewise.
	* testsuite/ld-x86-64/pr19609-7a.d: Likewise.
	* testsuite/ld-x86-64/pr19609-7c.d: Likewise.
2021-02-04 06:06:44 -08:00
H.J. Lu
37707bd822 ld: Restore PR ld/15146 tests
commit 7d409ac001
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Feb 4 13:56:34 2021 +1030

    PR27311, (symbol from plugin): undefined reference, hidden sym

fixed PR ld/15146.  Restore PR ld/15146 tests.

	PR ld/15146
	* testsuite/ld-plugin/lto.exp: Run PR ld/15146 tests.
	* testsuite/ld-plugin/pr15146.d: Restored.
	* testsuite/ld-plugin/pr15146a.c: Likewise.
	* testsuite/ld-plugin/pr15146b.c: Likewise.
	* testsuite/ld-plugin/pr15146c.c: Likewise.
	* testsuite/ld-plugin/pr15146d.c: Likewise.
2021-02-04 05:11:49 -08:00
Alan Modra
7d409ac001 PR27311, (symbol from plugin): undefined reference, hidden sym
bfd/
	PR 27311
	* elflink.c (elf_link_add_object_symbols): Don't pull in as-needed
	libraries for IR references on pass over libraries after LTO
	recompilation.
ld/
	* testsuite/ld-plugin/pr27311d.c: New test.
	* testsuite/ld-plugin/lto.exp: Rename pr27311 to pr27311-1, compile
	and link new test as pr27311-2.
2021-02-04 14:09:51 +10:30
Alan Modra
6955136728 PR27311 again, ld.bfd (symbol from plugin): undefined reference
bfd/
	PR 27311
	* elflink.c (_bfd_elf_add_default_symbol): Clear override when
	undecorated symbol will have a different version.
ld/
	* testsuite/ld-ifunc/ifunc.exp (libpr16467b.so, libpr16467bn.so):
	Link with --as-needed.
2021-02-03 13:09:47 +10:30
Alan Modra
9918bff7cf PR27311, ld.bfd (symbol from plugin): undefined reference
A default versioned symbol definition in a shared library is
overridden by an unversioned definition in a regular object file, and
thus should not be reason to make an as-needed library needed.

bfd/
	PR 27311
	* elflink.c (_bfd_elf_add_default_symbol): Add override parameter.
	Use when handling default versioned symbol.  Rename existing
	override variable to nondef_override and use for non-default
	versioned symbol.
	(elf_link_add_object_symbols): Adjust call to suit.  Don't
	pull in as-needed libraries when override is set.
ld/
	* testsuite/ld-plugin/pr27311.d,
	* testsuite/ld-plugin/pr27311.ver,
	* testsuite/ld-plugin/pr27311a.c,
	* testsuite/ld-plugin/pr27311b.c,
	* testsuite/ld-plugin/pr27311c.c: New testcase.
	* testsuite/ld-plugin/lto.exp: Run it.  Correct PR14918 and
	PR12982 entries.
2021-02-02 20:09:42 +10:30
Emery Hemingway
1f568f9a0d Add Genode target support
* configure.tgt: Add *-*-genode* as a target for AArch64 and x86.
2021-02-01 17:31:47 +00:00
Alan Modra
fb6c220ebd ld --defsym
This makes --defsym support the same expressions as assignment in a
script.  For example, --defsym 'HIDDEN(foo=0)', will define a hidden
visibility foo.

	* ldgram.y (defsym_expr): Use assignment rule.
	* ldlex.h (ldlex_defsym): Delete.
	* ldlex.l (DEFSYMEXP, ldlex_defsym): Delete.
2021-02-02 01:27:12 +10:30
Alan Modra
40726f16a8 ld script expression parsing
Parsing symbol or file/section names in ld linker scripts is a little
complicated.  Inside SECTIONS, a name might be the start of an
expression or an output section.  Is ".foo=x-y" a fancy section name
or is it the expression ".foo = x - y"?  It isn't possible for a
single lookahead parser to decide, so the answer in this case is
that it's a section name.  This is the reason why everyone writes
linker script assignment expressions with lots of white-space.

However, there are many places where the parser knows for sure that an
expression is expected.  Those could be written without whitespace
given the first change to ldlex.l below.  Unfortunately, that runs
into a lookahead problem.  Optional expressions at the end of an
output section statement require the parser to look ahead one token in
expression context.  For this example from standard scripts
  .interp             : { *(.interp) }
  .note.gnu.build-id  : { *(.note.gnu.build-id) }
at the end of the .interp closing brace, the parser is looking for
a possible memspec, phdr, fill or even an optional comma.  The next
token is a NAME, but in expression context that NAME now doesn't
include '-' as a valid char.  So the lookahead NAME is
".note.gnu.build" with an unexpected "-id" syntax error before the
colon.  The rest of the patch involving ldlex_backup arranges to
discard that NAME token so that it will be rescanned in the proper
script context.

	* ldgram.y (section): Call ldlex_backup.  Remove empty action.
	* ldlex.h (ldlex_backup): Declare.
	* ldlex.l (<EXPRESSION>NAME): Don't use NOCFILENAMECHAR set of
	chars, use SYMBOLNAMECHAR.
	(ldlex_backup): New function.
2021-02-02 01:27:12 +10:30
Alan Modra
a5f92c6756 ldgram.y low_level_library_NAME_list
Beginning a new rule hidden inside another rule is horrible.

	* ldgram.y: Whitespace fixes.
2021-02-01 18:35:33 +10:30
Alan Modra
49daa38f31 Re: ld: Add a test for PR ld/27259
* testsuite/ld-elf/pr27259.d: Correct sh_link match.
2021-02-01 18:35:33 +10:30
Alan Modra
c4566785ac PR27271, c6x-uclinux-ld segfaults linking ld-uClibc-1.0.37.so
bfd/
	PR 27271
	* elflink.c (bfd_elf_link_record_dynamic_symbol): Don't segfault
	on symbols defined in absolute or other special sections.
ld/
	* testsuite/ld-tic6x/tic6x.exp: Add pr27271 test.
2021-01-29 11:07:18 +10:30
H.J. Lu
53e556e5b4 ld: Add a test for PR ld/27259
PR ld/27259
	* testsuite/ld-elf/pr27259.d: New file.
	* testsuite/ld-elf/pr27259.s: Likewise.
2021-01-28 05:40:07 -08:00
Alan Modra
def97fb945 PR27259, SHF_LINK_ORDER self-link
This stops ld from endless looping on SHF_LINK_ORDER sh_link loops.

bfd/
	PR 27259
	* elflink.c (_bfd_elf_gc_mark_extra_sections): Use linker_mark to
	prevent endless looping of linked-to sections.
ld/
	PR 27259
	* ldelf.c (ldelf_before_place_orphans): Use linker_mark to
	prevent endless looping of linked-to sections.
2021-01-28 18:53:30 +10:30
H.J. Lu
2eda57ef61 ld: Fix a typo in testsuite/ld-x86-64/bnd-plt-1.d
Pass -mx86-used-note=no to assembler.

	* testsuite/ld-x86-64/bnd-plt-1.d: Fix a typo.
2021-01-26 16:18:41 -08:00
Alan Modra
1c9c9b9b55 PR27226, ld.bfd contains huge .rodata section
This makes it possible to build ld without any compiled-in scripts,
by setting COMPILE_IN=no in the environment.  pe, beos and pdp11
targets didn't support scripts from the file system, with pdp11
nastily editing the ld/ldscripts file so that the built-in script
didn't match.

	PR 27226
	* emulparams/alphavms.sh: Don't set COMPILE_IN.
	* emulparams/elf64_ia64_vms.sh: Likewise.
	* emulparams/elf64mmix.sh: Likewise.
	* emulparams/elf_iamcu.sh: Likewise.
	* emulparams/elf_k1om.sh: Likewise.
	* emulparams/elf_l1om.sh: Likewise.
	* emulparams/mmo.sh: Likewise.
	* emulparams/pdp11.sh: Set DATA_SEG_ADDR.
	* scripttempl/pdp11.sc: Use it.
	* emultempl/pdp11.em: Don't edit .xn script for separate_code,
	instead use .xe script.  Support scripts from file system.
	* emultempl/beos.em: Support scripts from file system.
	* emultempl/pe.em: Likewise.
	* emultempl/pep.em: Likewise.
	* testsuite/ld-bootstrap/bootstrap.exp: Make tmpdir/ldscripts link.
2021-01-26 20:54:43 +10:30
Fangrui Song
9e42b97628 Add some more DWARF-5 sections 2021-01-25 11:35:57 +00:00
Nick Clifton
b8df69003d Update linker scripts with the names of new DWARF-5 debug sections.
* scripttempl/DWARF.sc: Add .debug_loclists, .debug_rnglists,
	.debug_line_str and .debug_str_offsets.  Move .debug_macro and
	.debug_addr into DWARF-5 section.
2021-01-25 11:20:20 +00:00
Alan Modra
c78eec4424 mips XPASS pr26936
git commit 994b251328 "Ignore section symbols when matching linkonce
with comdat" cured the reason why this test used to fail on mips.

	* testsuite/ld-elf/pr26936.d: No longer xfail mips.
2021-01-21 16:48:35 +10:30
Alan Modra
4bd7c90276 PowerPC: Don't generate unused section symbols
PowerPC version of git commit d1bcae833b.

bfd/
	* elf32-ppc.c: Delete outdated comment.
	(TARGET_KEEP_UNUSED_SECTION_SYMBOLS): Define.
	* elf64-ppc.c (TARGET_KEEP_UNUSED_SECTION_SYMBOLS): Define.
gas/
	* testsuite/gas/ppc/power4.d: Adjust for removal of section sym.
	* testsuite/gas/ppc/test1elf32.d: Likewise.
	* testsuite/gas/ppc/test1elf64.d: Likewise.
ld/
	* testsuite/ld-powerpc/relbrlt.s: Make symbols global.
	* testsuite/ld-powerpc/relbrlt.d: Adjust to suit.
	* testsuite/ld-powerpc/tlsget.d: Adjust for reordered stubs.
	* testsuite/ld-powerpc/tlsget.wf: Likewise.
	* testsuite/ld-powerpc/tlsget2.d: Likewise.
	* testsuite/ld-powerpc/tlsget2.wf: Likewise.
	* testsuite/ld-powerpc/tlsexe.r: Adjust for removed section syms.
	* testsuite/ld-powerpc/tlsexe32.r: Likewise.
	* testsuite/ld-powerpc/tlsexe32no.r: Likewise.
	* testsuite/ld-powerpc/tlsexeno.r: Likewise.
	* testsuite/ld-powerpc/tlsexenors.r: Likewise.
	* testsuite/ld-powerpc/tlsexers.r: Likewise.
	* testsuite/ld-powerpc/tlsexetoc.r: Likewise.
	* testsuite/ld-powerpc/tlsexetocrs.r: Likewise.
	* testsuite/ld-powerpc/tlsso.r: Likewise.
	* testsuite/ld-powerpc/tlsso32.r: Likewise.
	* testsuite/ld-powerpc/tlstocso.r: Likewise.
2021-01-20 16:12:06 +10:30
Nick Alcock
c98de297b3 libctf, ld: fix data symbol test with newer GCC
GCC 11+ spots that the extern var_1 and var_666 declarations in this
test are unused, and removes them, thus stopping them from appearing as
conflicted data symbols and rendering the test pointless.  Use them in a
function unique to this TU to prevent them from being eliminated.

ld/ChangeLog
2021-01-19  Nick Alcock  <nick.alcock@oracle.com>

	* testsuite/ld-ctf/data-func-2.c: Stop removal of the extern foo_t
	symbols by the optimizer.
	* testsuite/ld-ctf/data-func-conflicted.d: Adjust accordingly.
2021-01-19 12:45:18 +00:00
Nelson Chu
85e963f185 ld: Just xfail riscv little endian targets for compressed1d.d test.
The sizes of compressed and uncompressed .debug_aranges are the same
for the riscv big endian targets, but different for the little endian
targets.  The compress rule is fine and isn't broken by riscv, just the
original compressed1d.d fails by accident.  Ideally, we should fill the
R_RISCV_ADD/SUB relocations when disabling relaxations in the assembler.
But before that, Jim already had written an alternative test compressed1d-alt
to relpace compressed1d.d for riscv, so we can only xfail the riscv little
endian targets in the short-term.

ld/
    * testsuite/ld-elf/elf.exp (riscv_little_endian): Added.  Return true
    if target is riscv little endian.  Otherwise, return false.
    * testsuite/ld-elf/compressed1d.d: Only xfail the riscv little endian
    targets by [riscv_little_endian].
2021-01-19 09:40:18 +08:00
Alan Modra
326adec374 PR26378, sections initialised only by linker scripts are always read/write
This changes the initialisation of output sections so that it is
possible to create read-only sections fed only from linker script
BYTE, SHORT, LONG or QUAD.  That currently isn't possible even for one
of the well-known ELF sections like .rodata, because once a section is
marked read/write that sticks.  On the other hand if we start
read-only, well-known ELF sections end up read/write as appropriate.
For example .tdata will still be SHF_ALLOC + SHF_WRITE + SHF_TLS.

	PR 26378
	* ldlang.c (map_input_to_output_sections): Start with a read-only
	section for data statements.
	* testsuite/ld-elf/size-2.d: Adjust to suit.
2021-01-18 23:07:40 +10:30
H.J. Lu
994b251328 ld/elf: Ignore section symbols when matching linkonce with comdat
When deciding if a single member comdat group section in file FOO should
be discarded by a linkonce section in file BAR, we check if 2 sections
define the same set of local and global symbols.  When only one of the
files doesn't contain the unused section symbols in the symbol table,
such as object files generated by clang or GNU assembler with

commit d1bcae833b
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 7 06:42:00 2021 -0800

    ELF: Don't generate unused section symbols

the check will fail since one file has the extra unused section symbols.
We should ignore both undefined and section symbols in the symbol table
when making such a decision.

bfd/

	PR ld/27193
	* elflink.c (elf_create_symbuf): Also ignore section symbols.

ld/

	PR ld/27193
	* testsuite/ld-i386/i386.exp: Run PR ld/27193 test.
	* testsuite/ld-i386/pr27193.dd: New file.
	* testsuite/ld-i386/pr27193a.o.bz2: Likewise.
	* testsuite/ld-i386/pr27193b.s: Likewise.
2021-01-17 20:01:30 -08:00
H.J. Lu
edf0f284b1 PR binutils/23460: Increase the max number of open files to 20
Increase the max number of open files to 20 for PR binutils/23460 test
which may have more than 16 file descriptors open:

lr-x------ 1 hjl hjl 64 Jan 16 16:49 0 -> /dev/null
l-wx------ 1 hjl hjl 64 Jan 16 16:49 1 -> pipe:[14151430]
lr-x------ 1 hjl hjl 64 Jan 16 16:49 10 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460c.o
lr-x------ 1 hjl hjl 64 Jan 16 16:49 11 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460d.o
lr-x------ 1 hjl hjl 64 Jan 16 16:49 12 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460e.o
lr-x------ 1 hjl hjl 64 Jan 16 16:49 13 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460f.o
lrwx------ 1 hjl hjl 64 Jan 16 16:49 14 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/stTLiXpO
lrwx------ 1 hjl hjl 64 Jan 16 16:49 15 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/stTLiXpO
l-wx------ 1 hjl hjl 64 Jan 16 16:49 2 -> pipe:[14151430]
lr-x------ 1 hjl hjl 64 Jan 16 16:49 3 -> pipe:[13933216]
l-wx------ 1 hjl hjl 64 Jan 16 16:49 4 -> pipe:[13933216]
lr-x------ 1 hjl hjl 64 Jan 16 16:49 5 -> pipe:[13990857]
l-wx------ 1 hjl hjl 64 Jan 16 16:49 6 -> pipe:[13990857]
lr-x------ 1 hjl hjl 64 Jan 16 16:49 7 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/libpr23460.a
lr-x------ 1 hjl hjl 64 Jan 16 16:49 8 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460a.o
lr-x------ 1 hjl hjl 64 Jan 16 16:49 9 -> /export/build/gnu/tools-build/x86_64-linux-toolchain/build-x86_64-linux/binutils/src/binutils-build/ld/tmpdir/pr23460b.o

	PR binutils/23460
	* testsuite/ld-plugin/lto.exp: Increase the max number of open
	files to 20 for PR binutils/23460 test.
2021-01-16 18:36:54 -08:00
H.J. Lu
75a933f399 ld/elf/x86: Don't compare IFUNC address in the shared object
On x86, glibc 2.33 starts to issue a fatal error message when calling
IFUNC function defined in the unrelocated executable from a shared
library.

1. Update x86 ELF linker to always convert IFUNC function defined in
position-dependent executable (PDE) to the normal function.  GOT in PDE
will be updated by R_*_IRELATIVE at run-time.
2. Update PR ld/23169 tests not to compare function address of external
IFUNC function in the shared object to avoid calling the IFUNC function
defined in the unrelocated executable.
3. Remove pr23169e tests which call the IFUNC function defined in the
unrelocated position-independent executable from a shared library.

bfd/

	PR ld/23169
	* elfxx-x86.c (_bfd_x86_elf_link_fixup_ifunc_symbol): Don't
	check pointer_equality_needed.

ld/

	PR ld/23169
	* testsuite/ld-ifunc/ifunc.exp: Replace pr23169c.rd with
	pr23169a.rd for pr23169c and pr23169f.  Remove pr23169e tests.
	* testsuite/ld-ifunc/pr23169a.c (foo): Don't compare function
	address.
2021-01-16 07:02:36 -08:00
H.J. Lu
68b007788a ld/x86: Add -z report-relative-reloc
Linker generated relative relocations don't have symbol info:

[hjl@gnu-cfl-2 tmpdir]$ readelf -rW dump

Relocation section '.rela.dyn' at offset 0x180 contains 2 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00002f70  00000008 R_X86_64_RELATIVE                 1000
00002f68  00000025 R_X86_64_IRELATIVE                1007
[hjl@gnu-cfl-2 tmpdir]$

Add -z report-relative-reloc to x86 ELF linker to report dynamic relative
relocations:

[hjl@gnu-cfl-2 tmpdir]$ ../ld-new -pie -melf32_x86_64 -z report-relative-reloc -o dump report-reloc-1.o
dump: R_X86_64_IRELATIVE (offset: 0x00002f68, info: 0x00000025, addend: 0x00001007) against 'ifunc' for section '.data.rel.ro.local' in report-reloc-1.o
dump: R_X86_64_RELATIVE (offset: 0x00002f70, info: 0x00000008, addend: 0x00001000) against '_start' for section '.data.rel.ro.local' in report-reloc-1.o
[hjl@gnu-cfl-2 tmpdir]$

It can be used to map relative relocations to corresponding symbols.

bfd/

	* elf-linker-x86.h (elf_linker_x86_params): Add
	report_relative_reloc.
	* elf32-i386.c (elf_i386_relocate_section): Call
	_bfd_x86_elf_link_report_relative_reloc to report relative
	relocations for -z report-relative-reloc.
	(elf_i386_finish_dynamic_symbol): Likewse.
	* elf64-x86-64.c (elf_x86_64_relocate_section): Likewse.
	(elf_x86_64_finish_dynamic_symbol): Likewse.
	* elfxx-x86.c (_bfd_x86_elf_link_report_relative_reloc): New
	function.
	* elfxx-x86.h (_bfd_x86_elf_link_report_relative_reloc): New
	prototype.

ld/

	* NEWS: Mention -z report-relative-reloc.
	* ld.texi: Document -z report-relative-reloc.
	* emulparams/elf32_x86_64.sh: Source x86-report-relative.sh.
	* emulparams/elf_i386.sh: Likewse.
	* emulparams/elf_x86_64.sh: Likewse.
	* emulparams/x86-report-relative.sh: New file.
	* testsuite/ld-i386/report-reloc-1.d: Likewse.
	* testsuite/ld-i386/report-reloc-1.l: Likewse.
	* testsuite/ld-i386/report-reloc-1.s: Likewse.
	* testsuite/ld-x86-64/report-reloc-1-x32.d: Likewse.
	* testsuite/ld-x86-64/report-reloc-1.d: Likewse.
	* testsuite/ld-x86-64/report-reloc-1.l: Likewse.
	* testsuite/ld-x86-64/report-reloc-1.s: Likewse.
	* testsuite/ld-i386/i386.exp: Run report-reloc-1.
	* testsuite/ld-x86-64/x86-64.exp: Run report-reloc-1 and
	report-reloc-1-x32.
2021-01-15 18:07:07 -08:00