public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Richard Sandiford <rsandifo@nildram.co.uk>
To: binutils@sourceware.org
Subject: Fix --gc-sections for C++ MIPS ELF
Date: Sun, 02 Dec 2007 20:15:00 -0000	[thread overview]
Message-ID: <87r6i4svjg.fsf@firetop.home> (raw)

--Wl,--gc-sections generates wrong code for C++ on MIPS ELF.  The problem
is that if:

  (a) all C++ CIEs have relocations against the personality routine
      itself, rather relocations against a pointer to the personality
      routine, and

  (b) as usual, there are no references to the personality routine's
      section outside .eh_frame

then we never mark the personality routine's section as needed.
The main code for marking .eh_frames is:

      /* Keep .gcc_except_table.* if the associated .text.* (or the
	 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
	 marked.  This isn't very nice, but the proper solution,
	 splitting .eh_frame up and using comdat doesn't pan out
	 easily due to needing special relocs to handle the
	 difference of two symbols in separate sections.
	 Don't keep code sections referenced by .eh_frame.  */
#define TEXT_PREFIX			".text."
#define TEXT_PREFIX2			".gnu.linkonce.t."
#define GCC_EXCEPT_TABLE_PREFIX		".gcc_except_table."
      for (o = sub->sections; o != NULL; o = o->next)
	if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)

and it ignores all relocations against code.

Non-PIC CIEs for targets like x86_64-linux-gnu also have direct references
to the personality routines.  I suspect the only reason -Wl,--gc-sections
-static-libgcc works for them is that libgcc's own CIEs use an indirect
reference, so the section gets marked that way.

A simple fix would be to use indirect references for MIPS ELF too,
but it would be nice to avoid the overhead.  It would also be nice to
make binutils work with older GCCs if possible.

Another reason to prefer a binutils change is that the current code
seems unsafe in ways that aren't explicitly mentioned in the comment
above.  The marking code assumes that FDEs against discarded sections will
themselves be discarded, but if an .eh_frame section contains something
unexpected, like a future augmentation type, elf-eh-frame.c will keep
the section as-is.  We would then get a link failure or silent wrong code.

Has there been any talk about parsing the CIEs and FDEs and marking
relocations against them individually?  I couldn't find anything in the
archives, so I gave it a go, and it seems to work.  The main problem was
that we currently don't parse the .eh_frame information until later in
the link process, so a fair amount of rejigging was needed.

Once we have access to the parsed information, we can mark an .eh_frame
relocation if:

  (i) it is associated with an FDE that is itself associated with
      a marked section or

  (ii) it is associated with the CIE for such an FDE.

I tested the changes by running the C++ and libstdc++-v3 testsuites
on x86_64-linux-gnu and mipsisa64-elf with -Wl,--gc-sections.
The C++ PCH tests failed, but the results were otherwise identical
to those without -Wl,--gc-sections.  There were also no regressions
in the binutils, gas and ld testsuites for x86_64-linux-gnu,
mipsisa64-elf and mips64-linux-gnu.

Because several changes are needed, I've tried to split things up for
ease of review.  I'll post each patch as a follow-up.

Richard

             reply	other threads:[~2007-12-02 20:15 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-02 20:15 Richard Sandiford [this message]
2007-12-02 20:18 ` [PATCH 1/7] " Richard Sandiford
2007-12-02 20:19 ` [PATCH 2/7] " Richard Sandiford
2007-12-02 20:28 ` [PATCH 3/7] " Richard Sandiford
2007-12-02 20:31 ` [PATCH 4/7] " Richard Sandiford
2007-12-02 20:35 ` [PATCH 5/7] " Richard Sandiford
2007-12-02 20:37 ` [PATCH 6/7] " Richard Sandiford
2007-12-02 20:39 ` [PATCH 7/7] " Richard Sandiford
2007-12-02 21:55 ` Jakub Jelinek
2007-12-04 10:08   ` Richard Sandiford
2007-12-02 22:26 ` Eric Botcazou
2007-12-04 10:02   ` [PATCH 8/7] " Richard Sandiford
2007-12-04 11:39     ` Eric Botcazou
2007-12-15  1:46 ` Alan Modra
2007-12-15  9:46   ` Richard Sandiford

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r6i4svjg.fsf@firetop.home \
    --to=rsandifo@nildram.co.uk \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).