On 29/10/2010 02:31, Ian Lance Taylor wrote: > This implements an object file reader/writer which does everything > required by LTO and gccgo. The ELF code works. I have not tested the > Mach-O and COFF code at all beyond compiling it; I hope that somebody > else can test those targets and fix them. > > With this patch, libelf is no longer needed.
Almost, but not quite! The attached patch switches the lto-plugin over to use the new objfile reader as well. lto-plugin/ChangeLog: * configure.ac: Don't use libelf, don't source config.gcc. (LIBELFLIBS): Delete. (LIBELFINC): Delete. (LTO_FORMAT): Delete. (SYM_STYLE): New AC_SUBST var set based on $target. * Makefile.am (LIBELFLIBS): Delete. (LIBELFINC): Delete. (LTO_FORMAT): Delete. (SYM_STYLE): Import. (AM_CPPFLAGS): Use it. Don't use LIBELFINC. (liblto_plugin_la_SOURCES): Don't use LTO_FORMAT, don't include any object-format-specific source file in the link. (liblto_plugin_la_LIBADD): Don't use LIBELFLIBS. * configure: Regenerate. * Makefile.in: Regenerate. * lto-plugin-elf.c: Delete. * lto-plugin-coff.c: Delete. * lto-plugin.h: Delete. * lto-plugin.c (O_BINARY): Definition moved here from lto-plugin.h. (LTO_SEGMENT_NAME): New definition. (LTO_SECTION_PREFIX): Definition moved here from lto-plugin.h. (LTO_SECTION_PREFIX_LEN): New definition. (struct sym_aux): Struct definition moved here from lto-plugin.h. (struct plugin_symtab): Likewise. (struct plugin_objfile): Likewise. (struct plugin_objfile): New struct def. (enum symbol_style): New enum type. (add_symbols): Make static. (claimed_files): Likewise. (num_claimed_files): Likewise. (sym_style): New global. (check): Make static. (parse_table_entry): Likewise. Respect sym_style when extracting symbol from symtab entry. (translate): Make static. (resolve_conflicts): Likewise. (process_symtab): New function, per-section callback version of old object-format-specific handling from deleted lto-plugin-elf.c. (claim_file_handler): Convert ELF-specific version from deleted lto-plugin-elf.c to objfile interface and move here. (process_options): Allow new '-sym-style=' option. (onload): Don't call deleted onload_format_checks hook. Bootstrapped and tested all languages except java and ada on x86_64-unknown-linux-gnu, no regressions. There are still a couple of bugs to iron out of the COFF-specific part of the objfile reader which I'll have fixed later today. OK for trunk (once the objfile changes have gone in)? cheers, DaveK
Index: lto-plugin/lto-plugin-elf.c =================================================================== --- lto-plugin/lto-plugin-elf.c (revision 166059) +++ lto-plugin/lto-plugin-elf.c (working copy) @@ -1,157 +0,0 @@ -/* LTO plugin for gold. - Copyright (C) 2009, 2010 Free Software Foundation, Inc. - Contributed by Rafael Avila de Espindola (espind...@google.com). - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3, or (at your option) -any later version. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; see the file COPYING3. If not see -<http://www.gnu.org/licenses/>. */ - -#include <assert.h> -#include <string.h> -#include <stdio.h> -#include <libiberty.h> -#include <stdlib.h> -#include <inttypes.h> - -/* The presence of gelf.h is checked by the toplevel configure script. */ -#include <gelf.h> - -/* Common definitions that the object format dependent code needs. */ -#include "lto-plugin.h" - -/* Process all lto symtabs of file ELF. */ - -static int -process_symtab (Elf *elf, struct plugin_symtab *out) -{ - int found = 0; - Elf_Scn *section = 0; - GElf_Ehdr header; - GElf_Ehdr *t = gelf_getehdr (elf, &header); - if (t == NULL) - return 0; - assert (t == &header); - - while ((section = elf_nextscn(elf, section)) != 0) - { - GElf_Shdr shdr; - GElf_Shdr *tshdr = gelf_getshdr (section, &shdr); - Elf_Data *symtab; - const char *t; - assert (tshdr == &shdr); - t = elf_strptr (elf, header.e_shstrndx, shdr.sh_name); - assert (t != NULL); - if (strncmp (t, LTO_SECTION_PREFIX, strlen (LTO_SECTION_PREFIX)) == 0) - { - char *s = strrchr (t, '.'); - if (s) - sscanf (s, ".%x", &out->id); - symtab = elf_getdata (section, NULL); - translate (symtab->d_buf, symtab->d_buf + symtab->d_size, out); - found++; - } - } - return found; -} - -/* Callback used by gold to check if the plugin will claim FILE. Writes - the result in CLAIMED. */ - -enum ld_plugin_status -claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) -{ - enum ld_plugin_status status; - Elf *elf; - struct plugin_file_info lto_file; - int n; - - memset (<o_file, 0, sizeof (struct plugin_file_info)); - - if (file->offset != 0) - { - char *objname; - Elf *archive; - off_t offset; - /* We pass the offset of the actual file, not the archive header. */ - int t = asprintf (&objname, "%...@0x%" PRIx64, file->name, - (int64_t) file->offset); - check (t >= 0, LDPL_FATAL, "asprintf failed"); - lto_file.name = objname; - - archive = elf_begin (file->fd, ELF_C_READ, NULL); - check (elf_kind (archive) == ELF_K_AR, LDPL_FATAL, - "Not an archive and offset not 0"); - - /* elf_rand expects the offset to point to the ar header, not the - object itself. Subtract the size of the ar header (60 bytes). - We don't uses sizeof (struct ar_hd) to avoid including ar.h */ - - offset = file->offset - 60; - check (offset == elf_rand (archive, offset), LDPL_FATAL, - "could not seek in archive"); - elf = elf_begin (file->fd, ELF_C_READ, archive); - check (elf != NULL, LDPL_FATAL, "could not find archive member"); - elf_end (archive); - } - else - { - lto_file.name = xstrdup (file->name); - elf = elf_begin (file->fd, ELF_C_READ, NULL); - } - lto_file.handle = file->handle; - - *claimed = 0; - - if (!elf) - goto err; - - n = process_symtab (elf, <o_file.symtab); - if (n == 0) - goto err; - - if (n > 1) - resolve_conflicts (<o_file.symtab, <o_file.conflicts); - - status = add_symbols (file->handle, lto_file.symtab.nsyms, - lto_file.symtab.syms); - check (status == LDPS_OK, LDPL_FATAL, "could not add symbols"); - - *claimed = 1; - num_claimed_files++; - claimed_files = - xrealloc (claimed_files, - num_claimed_files * sizeof (struct plugin_file_info)); - claimed_files[num_claimed_files - 1] = lto_file; - - goto cleanup; - - err: - free (lto_file.name); - - cleanup: - if (elf) - elf_end (elf); - - return LDPS_OK; -} - -/* Method called first thing at onload time to perform sanity checks. */ - -enum ld_plugin_status -onload_format_checks (struct ld_plugin_tv *tv) -{ - unsigned version = elf_version (EV_CURRENT); - check (version != EV_NONE, LDPL_FATAL, "invalid ELF version"); - return LDPS_OK; -} - Index: lto-plugin/lto-plugin.h =================================================================== --- lto-plugin/lto-plugin.h (revision 166059) +++ lto-plugin/lto-plugin.h (working copy) @@ -1,84 +0,0 @@ -/* Common declarations for LTO plugin for gold and/or GNU ld. - Copyright (C) 2009, 2010 Free Software Foundation, Inc. - Contributed by Rafael Avila de Espindola (espind...@google.com). - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3, or (at your option) -any later version. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; see the file COPYING3. If not see -<http://www.gnu.org/licenses/>. */ - -#include <stdbool.h> -#include "plugin-api.h" - -/* LTO magic section name. */ - -#define LTO_SECTION_PREFIX ".gnu.lto_.symtab" - -/* The part of the symbol table the plugin has to keep track of. Note that we - must keep SYMS until all_symbols_read is called to give the linker time to - copy the symbol information. */ - -struct sym_aux -{ - uint32_t slot; - unsigned id; - unsigned next_conflict; -}; - -struct plugin_symtab -{ - int nsyms; - struct sym_aux *aux; - struct ld_plugin_symbol *syms; - unsigned id; -}; - -/* All that we have to remember about a file. */ - -struct plugin_file_info -{ - char *name; - void *handle; - struct plugin_symtab symtab; - struct plugin_symtab conflicts; -}; - -/* These are the methods supplied by one of the object format - dependent files lto-plugin-elf.c or lto-plugin-coff.c */ - -extern enum ld_plugin_status claim_file_handler - (const struct ld_plugin_input_file *file, int *claimed); - -extern enum ld_plugin_status onload_format_checks (struct ld_plugin_tv *tv); - -/* These methods are made available to the object format - dependent files. */ - -extern void check (bool gate, enum ld_plugin_level level, const char *text); - -extern void translate (char *data, char *end, struct plugin_symtab *out); - -extern char *parse_table_entry (char *p, struct ld_plugin_symbol *entry, - struct sym_aux *aux); - -extern void resolve_conflicts (struct plugin_symtab *t, - struct plugin_symtab *conflicts); - -/* And this callback function is exposed. */ - -extern ld_plugin_add_symbols add_symbols; - -/* Along with these two variables. */ - -extern struct plugin_file_info *claimed_files; -extern unsigned int num_claimed_files; - Index: lto-plugin/configure.ac =================================================================== --- lto-plugin/configure.ac (revision 166059) +++ lto-plugin/configure.ac (working copy) @@ -6,18 +6,21 @@ AM_MAINTAINER_MODE AC_PROG_CC AC_SYS_LARGEFILE -AC_ARG_VAR(LIBELFLIBS,[How to link libelf]) -AC_ARG_VAR(LIBELFINC,[How to find libelf include files]) AM_PROG_LIBTOOL AC_SUBST(target_noncanonical) -. ${srcdir}/../gcc/config.gcc -case ${lto_binary_reader} in - *coff*) LTO_FORMAT=coff ;; - *elf*) LTO_FORMAT=elf ;; - *) AC_MSG_ERROR([LTO plugin is not supported on this target.]) ;; +# Trying to get this information from gcc's config is tricky. +case $target in + x86_64*-mingw*) + SYM_STYLE=-DSYM_STYLE=ss_none + ;; + *-cygwin* | i?86*-mingw* ) + SYM_STYLE=-DSYM_STYLE=ss_win32 + ;; + *) + SYM_STYLE=-DSYM_STYLE=ss_none + ;; esac - -AC_SUBST(LTO_FORMAT) +AC_SUBST(SYM_STYLE) AC_TYPE_UINT64_T AC_CONFIG_FILES(Makefile) AC_OUTPUT Index: lto-plugin/lto-plugin-coff.c =================================================================== --- lto-plugin/lto-plugin-coff.c (revision 166059) +++ lto-plugin/lto-plugin-coff.c (working copy) @@ -1,38 +0,0 @@ -/* LTO plugin for gold. - Copyright (C) 2010 Free Software Foundation, Inc. - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3, or (at your option) -any later version. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; see the file COPYING3. If not see -<http://www.gnu.org/licenses/>. */ - -/* Common definitions that the object format dependent code needs. */ -#include "lto-plugin.h" - -/* Callback used by gold to check if the plugin will claim FILE. Writes - the result in CLAIMED. */ - -enum ld_plugin_status -claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) -{ - /* To be implemented; for now, simply do nothing. */ - return LDPS_OK; -} - -/* Method called first thing at onload time to perform sanity checks. */ - -enum ld_plugin_status -onload_format_checks (struct ld_plugin_tv *tv) -{ - return LDPS_OK; -} - Index: lto-plugin/Makefile.am =================================================================== --- lto-plugin/Makefile.am (revision 166059) +++ lto-plugin/Makefile.am (working copy) @@ -7,20 +7,16 @@ target_noncanonical := @target_noncanonical@ libexecsubdir := $(libexecdir)/gcc/$(target_noncanonical)/$(gcc_version) -# How to find libelf -LIBELFLIBS = @LIBELFLIBS@ -LIBELFINC = @LIBELFINC@ +# Which symbol format to use. +SYM_STYLE = @SYM_STYLE@ -# Which object format to parse. -LTO_FORMAT = @LTO_FORMAT@ - -AM_CPPFLAGS = -I$(top_srcdir)/../include $(LIBELFINC) +AM_CPPFLAGS = -I$(top_srcdir)/../include $(SYM_STYLE) AM_CFLAGS = -Wall -Werror libexecsub_LTLIBRARIES = liblto_plugin.la -liblto_plugin_la_SOURCES = lto-plugin.c lto-plugin-$(LTO_FORMAT).c -liblto_plugin_la_LIBADD = $(LIBELFLIBS) \ +liblto_plugin_la_SOURCES = lto-plugin.c +liblto_plugin_la_LIBADD = \ $(if $(wildcard ../libiberty/pic/libiberty.a),../libiberty/pic/libiberty.a,) liblto_plugin_la_LDFLAGS = -no-undefined -bindir $(libexecsubdir) \ $(if $(wildcard ../libiberty/pic/libiberty.a),,-Wc,../libiberty/libiberty.a) Index: lto-plugin/lto-plugin.c =================================================================== --- lto-plugin/lto-plugin.c (revision 166059) +++ lto-plugin/lto-plugin.c (working copy) @@ -46,10 +46,73 @@ #include <libiberty.h> #include <hashtab.h> #include "../gcc/lto/common.h" +#include "objfile.h" +#include "plugin-api.h" -/* Common definitions for/from the object format dependent code. */ -#include "lto-plugin.h" +/* Handle opening elf files on hosts, such as Windows, that may use + text file handling that will break binary access. */ +#ifndef O_BINARY +# define O_BINARY 0 +#endif +/* Segment name for LTO sections. This is only used for Mach-O. + FIXME: This needs to be kept in sync with darwin.c. */ + +#define LTO_SEGMENT_NAME "__GNU_LTO" + +/* LTO magic section name. */ + +#define LTO_SECTION_PREFIX ".gnu.lto_.symtab" +#define LTO_SECTION_PREFIX_LEN (sizeof (LTO_SECTION_PREFIX) - 1) + +/* The part of the symbol table the plugin has to keep track of. Note that we + must keep SYMS until all_symbols_read is called to give the linker time to + copy the symbol information. */ + +struct sym_aux +{ + uint32_t slot; + unsigned id; + unsigned next_conflict; +}; + +struct plugin_symtab +{ + int nsyms; + struct sym_aux *aux; + struct ld_plugin_symbol *syms; + unsigned id; +}; + +/* Encapsulates object file data during symbol scan. */ +struct plugin_objfile +{ + int found; + objfile_read *objfile; + struct plugin_symtab *out; + const struct ld_plugin_input_file *file; +}; + +/* All that we have to remember about a file. */ + +struct plugin_file_info +{ + char *name; + void *handle; + struct plugin_symtab symtab; + struct plugin_symtab conflicts; +}; + +/* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from + stdio file streams, we do simple label translation here. */ + +enum symbol_style +{ + ss_none, /* No underscore prefix. */ + ss_win32, /* Underscore prefix any symbol not beginning with '@'. */ + ss_uscore, /* Underscore prefix all symbols. */ +}; + static char *arguments_file_name; static ld_plugin_register_claim_file register_claim_file; static ld_plugin_register_all_symbols_read register_all_symbols_read; @@ -58,14 +121,11 @@ static ld_plugin_add_input_file add_input_file; static ld_plugin_add_input_library add_input_library; static ld_plugin_message message; +static ld_plugin_add_symbols add_symbols; -/* These are not static because the object format dependent - claim_file hooks in lto-plugin-{coff,elf}.c need them. */ -ld_plugin_add_symbols add_symbols; +static struct plugin_file_info *claimed_files = NULL; +static unsigned int num_claimed_files = 0; -struct plugin_file_info *claimed_files = NULL; -unsigned int num_claimed_files = 0; - static char **output_files = NULL; static unsigned int num_output_files = 0; @@ -79,7 +139,12 @@ static bool nop; static char *resolution_file = NULL; -void +/* Set by default from configure.ac, but can be overridden at runtime + by using -plugin-opt=-sym-style={none,win32,underscore|uscore} + (in fact, only first letter of style arg is checked.) */ +static enum symbol_style sym_style = SYM_STYLE; + +static void check (bool gate, enum ld_plugin_level level, const char *text) { if (gate) @@ -100,7 +165,7 @@ by P and the result is written in ENTRY. The slot number is stored in SLOT. Returns the address of the next entry. */ -char * +static char * parse_table_entry (char *p, struct ld_plugin_symbol *entry, struct sym_aux *aux) { @@ -122,7 +187,24 @@ LDPV_HIDDEN }; - entry->name = xstrdup (p); + switch (sym_style) + { + case ss_win32: + if (p[0] == '@') + { + /* cf. Duff's device. */ + case ss_none: + entry->name = xstrdup (p); + break; + } + /* FALL-THROUGH. */ + case ss_uscore: + entry->name = concat ("_", p, NULL); + break; + default: + check (false, LDPL_FATAL, "invalid symbol style requested"); + break; + } while (*p) p++; p++; @@ -165,7 +247,7 @@ /* Translate the IL symbol table located between DATA and END. Append the slots and symbols to OUT. */ -void +static void translate (char *data, char *end, struct plugin_symtab *out) { struct sym_aux *aux; @@ -621,7 +703,7 @@ XXX how to handle common? */ -void +static void resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts) { htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL); @@ -689,6 +771,120 @@ htab_delete (symtab); } +/* Process one section of an object file. */ + +static int +process_symtab (void *data, const char *name, off_t offset, off_t length) +{ + struct plugin_objfile *obj = (struct plugin_objfile *)data; + char *s; + char *secdata; + + if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0) + return 1; + + s = strrchr (name, '.'); + if (s) + sscanf (s, ".%x", &obj->out->id); + secdata = xmalloc (length); + offset += obj->file->offset; + if (offset != lseek (obj->file->fd, offset, SEEK_SET) + || length != read (obj->file->fd, secdata, length)) + { + if (message) + message (LDPL_FATAL, "%s: corrupt object file", obj->file->name); + /* Force claim_file_handler to abandon this file. */ + obj->found = 0; + free (data); + return 0; + } + + translate (secdata, secdata + length, obj->out); + obj->found++; + free (data); + return 1; +} + +/* Callback used by gold to check if the plugin will claim FILE. Writes + the result in CLAIMED. */ + +static enum ld_plugin_status +claim_file_handler (const struct ld_plugin_input_file *file, int *claimed) +{ + enum ld_plugin_status status; + struct plugin_objfile obj; + struct plugin_file_info lto_file; + int err; + const char *errmsg; + + memset (<o_file, 0, sizeof (struct plugin_file_info)); + + if (file->offset != 0) + { + char *objname; + /* We pass the offset of the actual file, not the archive header. */ + int t = asprintf (&objname, "%...@0x%" PRIx64, file->name, + (int64_t) file->offset); + check (t >= 0, LDPL_FATAL, "asprintf failed"); + lto_file.name = objname; + } + else + { + lto_file.name = xstrdup (file->name); + } + lto_file.handle = file->handle; + + *claimed = 0; + obj.file = file; + obj.found = 0; + obj.out = <o_file.symtab; + errmsg = NULL; + obj.objfile = objfile_open_read (file->fd, file->offset, LTO_SEGMENT_NAME, + &errmsg, &err); + if (obj.objfile) + errmsg = objfile_find_sections (obj.objfile, process_symtab, &obj, &err); + + if (!obj.objfile || errmsg) + { + if (err && message) + message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg, + xstrerror (err)); + else if (message) + message (LDPL_FATAL, "%s: %s", file->name, errmsg); + goto err; + } + + if (obj.found == 0) + goto err; + + if (obj.found > 1) + resolve_conflicts (<o_file.symtab, <o_file.conflicts); + + status = add_symbols (file->handle, lto_file.symtab.nsyms, + lto_file.symtab.syms); + check (status == LDPS_OK, LDPL_FATAL, "could not add symbols"); + + *claimed = 1; + num_claimed_files++; + claimed_files = + xrealloc (claimed_files, + num_claimed_files * sizeof (struct plugin_file_info)); + claimed_files[num_claimed_files - 1] = lto_file; + + goto cleanup; + + err: + free (lto_file.name); + + cleanup: + if (obj.objfile) + objfile_release_read (obj.objfile); + if (file->fd >= 0) + close (file->fd); + + return LDPS_OK; +} + /* Parse the plugin options. */ static void @@ -706,6 +902,21 @@ pass_through_items[num_pass_through_items - 1] = xstrdup (option + strlen ("-pass-through=")); } + else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1)) + { + switch (option[sizeof ("-sym-style=")]) + { + case 'w': + sym_style = ss_win32; + break; + case 'u': + sym_style = ss_uscore; + break; + default: + sym_style = ss_none; + break; + } + } else { int size; @@ -727,10 +938,6 @@ struct ld_plugin_tv *p; enum ld_plugin_status status; - status = onload_format_checks (tv); - if (status != LDPS_OK) - return status; - p = tv; while (p->tv_tag) {