This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: RFA: Simpler get_relocated_section_contents interface
- From: Daniel Jacobowitz <drow at mvista dot com>
- To: binutils at sources dot redhat dot com
- Date: Wed, 2 Oct 2002 00:57:32 -0400
- Subject: Re: RFA: Simpler get_relocated_section_contents interface
- References: <20020927201054.GA4763@nevyn.them.org>
Anyone have a comment on this? Need it for both readelf and GDB,
eventually...
On Fri, Sep 27, 2002 at 04:10:54PM -0400, Daniel Jacobowitz wrote:
> This patch adds a new file, `simple.c', which is aimed at providing
> lightweight interfaces. This is designed for clients instead of linkers.
> It serves as a combination interface and programming example. These are
> things which it would probably be wiser to do with something like libelf;
> but I figure there's very little point in requiring _both_ libelf and BFD,
> when BFD should be entirely capable of these relatively simple jobs.
>
> The only method in it right now is bfd_simple_get_relocated_section_contents.
> Right now it supports only cases where all referenced symbols appear in the
> same BFD; it's very quiet about errors. This is targeted at relocating
> DWARF-2 information in readelf and GDB.
>
> It's not efficient. It adds the BFD's symbols to the hash table every time
> you relocate a session. I chose this added cost - relatively small number
> of debugging sections will be going through this mechanism - over the
> complexity of having begin and end routines. When something wants to use
> this interface for more than a handful of sections per BFD, then I'll
> generalize it; for now I don't think the cost is worthwhile. Simplicity was
> my king here. It's enough to meet GDB's needs, and I'll make readelf use it
> also.
>
> Does this look OK? Any prefered interface changes? Any other mechanisms
> that it would be nice to expose this way?
>
> [Oh, and I added linker.c's annotations to bfd-in2.h; it was missing from
> the list.]
>
> --
> Daniel Jacobowitz
> MontaVista Software Debian GNU/Linux Developer
>
> 2002-09-27 Daniel Jacobowitz <drow@mvista.com>
>
> * Makefile.am: Run dep-am.
> (BFD32_LIBS): Add simple.lo.
> (BFD32_LIBS_CFILES): Add simple.c.
> * Makefile.in: Regenerated.
> * bfd-in2.h: Regenerated.
> * simple.c: New file.
>
> 2002-09-27 Daniel Jacobowitz <drow@mvista.com>
>
> * Makefile.am (BFD_H_DEP): Add simple.c and linker.c.
> * Makefile.in: Regenerated.
>
> Index: bfd/Makefile.am
> ===================================================================
> RCS file: /cvs/src/src/bfd/Makefile.am,v
> retrieving revision 1.102
> diff -u -p -r1.102 Makefile.am
> --- bfd/Makefile.am 21 Sep 2002 09:59:19 -0000 1.102
> +++ bfd/Makefile.am 27 Sep 2002 19:56:26 -0000
> @@ -32,7 +32,7 @@ BFD32_LIBS = \
> format.lo init.lo libbfd.lo opncls.lo reloc.lo \
> section.lo syms.lo targets.lo hash.lo linker.lo \
> srec.lo binary.lo tekhex.lo ihex.lo stabs.lo stab-syms.lo \
> - merge.lo dwarf2.lo
> + merge.lo dwarf2.lo simple.lo
>
> BFD64_LIBS = archive64.lo
>
> @@ -41,7 +41,7 @@ BFD32_LIBS_CFILES = \
> format.c init.c libbfd.c opncls.c reloc.c \
> section.c syms.c targets.c hash.c linker.c \
> srec.c binary.c tekhex.c ihex.c stabs.c stab-syms.c \
> - merge.c dwarf2.c
> + merge.c dwarf2.c simple.c
>
> BFD64_LIBS_CFILES = archive64.c
>
> @@ -900,6 +900,7 @@ merge.lo: merge.c $(INCDIR)/filenames.h
> dwarf2.lo: dwarf2.c $(INCDIR)/filenames.h $(INCDIR)/libiberty.h \
> elf-bfd.h $(INCDIR)/elf/common.h $(INCDIR)/elf/internal.h \
> $(INCDIR)/elf/external.h $(INCDIR)/bfdlink.h $(INCDIR)/elf/dwarf2.h
> +simple.lo: simple.c $(INCDIR)/filenames.h
> archive64.lo: archive64.c $(INCDIR)/filenames.h $(INCDIR)/aout/ar.h
> cpu-a29k.lo: cpu-a29k.c $(INCDIR)/filenames.h
> cpu-alpha.lo: cpu-alpha.c $(INCDIR)/filenames.h
> Index: bfd/Makefile.in
> ===================================================================
> RCS file: /cvs/src/src/bfd/Makefile.in,v
> retrieving revision 1.111
> diff -u -p -r1.111 Makefile.in
> --- bfd/Makefile.in 21 Sep 2002 09:59:19 -0000 1.111
> +++ bfd/Makefile.in 27 Sep 2002 19:56:27 -0000
> @@ -1,6 +1,6 @@
> -# Makefile.in generated automatically by automake 1.4-p5 from Makefile.am
> +# Makefile.in generated automatically by automake 1.4 from Makefile.am
>
> -# Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc.
> +# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
> # This Makefile.in is free software; the Free Software Foundation
> # gives unlimited permission to copy and/or distribute it,
> # with or without modifications, as long as this notice is preserved.
> @@ -156,7 +156,7 @@ BFD32_LIBS = \
> format.lo init.lo libbfd.lo opncls.lo reloc.lo \
> section.lo syms.lo targets.lo hash.lo linker.lo \
> srec.lo binary.lo tekhex.lo ihex.lo stabs.lo stab-syms.lo \
> - merge.lo dwarf2.lo
> + merge.lo dwarf2.lo simple.lo
>
>
> BFD64_LIBS = archive64.lo
> @@ -166,7 +166,7 @@ BFD32_LIBS_CFILES = \
> format.c init.c libbfd.c opncls.c reloc.c \
> section.c syms.c targets.c hash.c linker.c \
> srec.c binary.c tekhex.c ihex.c stabs.c stab-syms.c \
> - merge.c dwarf2.c
> + merge.c dwarf2.c simple.c
>
>
> BFD64_LIBS_CFILES = archive64.c
> @@ -785,7 +785,7 @@ LTLIBRARIES = $(bfdlib_LTLIBRARIES)
> libbfd_la_OBJECTS = archive.lo archures.lo bfd.lo cache.lo coffgen.lo \
> corefile.lo format.lo init.lo libbfd.lo opncls.lo reloc.lo section.lo \
> syms.lo targets.lo hash.lo linker.lo srec.lo binary.lo tekhex.lo \
> -ihex.lo stabs.lo stab-syms.lo merge.lo dwarf2.lo archive64.lo
> +ihex.lo stabs.lo stab-syms.lo merge.lo dwarf2.lo simple.lo archive64.lo
> CFLAGS = @CFLAGS@
> COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
> LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
> @@ -949,7 +949,7 @@ maintainer-clean-recursive:
> dot_seen=no; \
> rev=''; list='$(SUBDIRS)'; for subdir in $$list; do \
> rev="$$subdir $$rev"; \
> - test "$$subdir" != "." || dot_seen=yes; \
> + test "$$subdir" = "." && dot_seen=yes; \
> done; \
> test "$$dot_seen" = "no" && rev=". $$rev"; \
> target=`echo $@ | sed s/-recursive//`; \
> @@ -1430,6 +1430,7 @@ merge.lo: merge.c $(INCDIR)/filenames.h
> dwarf2.lo: dwarf2.c $(INCDIR)/filenames.h $(INCDIR)/libiberty.h \
> elf-bfd.h $(INCDIR)/elf/common.h $(INCDIR)/elf/internal.h \
> $(INCDIR)/elf/external.h $(INCDIR)/bfdlink.h $(INCDIR)/elf/dwarf2.h
> +simple.lo: simple.c $(INCDIR)/filenames.h
> archive64.lo: archive64.c $(INCDIR)/filenames.h $(INCDIR)/aout/ar.h
> cpu-a29k.lo: cpu-a29k.c $(INCDIR)/filenames.h
> cpu-alpha.lo: cpu-alpha.c $(INCDIR)/filenames.h
> Index: bfd/bfd-in2.h
> ===================================================================
> RCS file: /cvs/src/src/bfd/bfd-in2.h,v
> retrieving revision 1.171
> diff -u -p -r1.171 bfd-in2.h
> --- bfd/bfd-in2.h 19 Sep 2002 19:01:11 -0000 1.171
> +++ bfd/bfd-in2.h 27 Sep 2002 19:56:28 -0000
> @@ -1,7 +1,7 @@
> /* DO NOT EDIT! -*- buffer-read-only: t -*- This file is automatically
> generated from "bfd-in.h", "init.c", "opncls.c", "libbfd.c",
> "section.c", "archures.c", "reloc.c", "syms.c", "bfd.c", "archive.c",
> - "corefile.c", "targets.c" and "format.c".
> + "corefile.c", "targets.c", "format.c", "linker.c" and "simple.c".
> Run "make headers" in your build bfd/ to regenerate. */
>
> /* Main header file for the bfd library -- portable access to object files.
> @@ -4109,6 +4109,17 @@ bfd_set_format PARAMS ((bfd *abfd, bfd_f
>
> const char *
> bfd_format_string PARAMS ((bfd_format format));
> +
> +/* Extracted from linker.c. */
> +boolean
> +bfd_link_split_section PARAMS ((bfd *abfd, asection *sec));
> +
> +#define bfd_link_split_section(abfd, sec) \
> + BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
> +
> +/* Extracted from simple.c. */
> +bfd_byte *
> +bfd_simple_get_relocated_section_contents PARAMS ((bfd *abfd, asection *sec, bfd_byte *outbuf));
>
> #ifdef __cplusplus
> }
> Index: bfd/simple.c
> ===================================================================
> RCS file: bfd/simple.c
> diff -N bfd/simple.c
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ bfd/simple.c 27 Sep 2002 19:56:28 -0000
> @@ -0,0 +1,166 @@
> +/* simple.c -- BFD simple client routines
> + Copyright 2002
> + Free Software Foundation, Inc.
> + Contributed by MontaVista Software, Inc.
> +
> + This file is part of BFD, the Binary File Descriptor library.
> +
> + 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 2 of the License, 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; if not, write to the Free Software
> + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
> +
> +#include "bfd.h"
> +#include "sysdep.h"
> +#include "libbfd.h"
> +#include "bfdlink.h"
> +
> +static boolean
> +simple_dummy_warning (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
> + const char *warning ATTRIBUTE_UNUSED,
> + const char *symbol ATTRIBUTE_UNUSED,
> + bfd *abfd ATTRIBUTE_UNUSED,
> + asection *section ATTRIBUTE_UNUSED,
> + bfd_vma address ATTRIBUTE_UNUSED)
> +{
> + return true;
> +}
> +
> +static boolean
> +simple_dummy_undefined_symbol (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
> + const char *name ATTRIBUTE_UNUSED,
> + bfd *abfd ATTRIBUTE_UNUSED,
> + asection *section ATTRIBUTE_UNUSED,
> + bfd_vma address ATTRIBUTE_UNUSED,
> + boolean fatal ATTRIBUTE_UNUSED)
> +{
> + return true;
> +}
> +
> +static boolean
> +simple_dummy_reloc_overflow (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
> + const char *name ATTRIBUTE_UNUSED,
> + const char *reloc_name ATTRIBUTE_UNUSED,
> + bfd_vma addend ATTRIBUTE_UNUSED,
> + bfd *abfd ATTRIBUTE_UNUSED,
> + asection *section ATTRIBUTE_UNUSED,
> + bfd_vma address ATTRIBUTE_UNUSED)
> +{
> + return true;
> +}
> +
> +static boolean
> +simple_dummy_reloc_dangerous (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
> + const char *message ATTRIBUTE_UNUSED,
> + bfd *abfd ATTRIBUTE_UNUSED,
> + asection *section ATTRIBUTE_UNUSED,
> + bfd_vma address ATTRIBUTE_UNUSED)
> +{
> + return true;
> +}
> +
> +static boolean
> +simple_dummy_unattached_reloc (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
> + const char *name ATTRIBUTE_UNUSED,
> + bfd *abfd ATTRIBUTE_UNUSED,
> + asection *section ATTRIBUTE_UNUSED,
> + bfd_vma address ATTRIBUTE_UNUSED)
> +{
> + return true;
> +}
> +
> +/*
> +FUNCTION
> + bfd_simple_relocate_secton
> +
> +SYNOPSIS
> + bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf);
> +
> +DESCRIPTION
> + Returns the relocated contents of section @var{sec}. Only symbols
> + from @var{abfd} and the output offsets assigned to sections in
> + @var{abfd} are used. The result will be stored at @var{outbuf}
> + or allocated with @code{bfd_malloc} if @var{outbuf} is @code{NULL}.
> +
> + Generally all sections in @var{abfd} should have their
> + @code{output_section} pointing back to the original section.
> +
> + Returns @code{NULL} on a fatal error; ignores errors applying
> + particular relocations.
> +*/
> +
> +bfd_byte *
> +bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec,
> + bfd_byte *outbuf)
> +{
> + struct bfd_link_info link_info;
> + struct bfd_link_order link_order;
> + struct bfd_link_callbacks callbacks;
> + bfd_byte *contents, *data;
> + int storage_needed, number_of_symbols;
> + asymbol **symbol_table;
> +
> + /* In order to use bfd_get_relocated_section_contents, we need
> + to forge some data structures that it expects. */
> +
> + /* Fill in the bare minimum number of fields for our purposes. */
> + memset (&link_info, 0, sizeof (link_info));
> + link_info.input_bfds = abfd;
> +
> + link_info.hash = bfd_link_hash_table_create (abfd);
> + link_info.callbacks = &callbacks;
> + callbacks.warning = simple_dummy_warning;
> + callbacks.undefined_symbol = simple_dummy_undefined_symbol;
> + callbacks.reloc_overflow = simple_dummy_reloc_overflow;
> + callbacks.reloc_dangerous = simple_dummy_reloc_dangerous;
> + callbacks.unattached_reloc = simple_dummy_unattached_reloc;
> +
> + memset (&link_order, 0, sizeof (link_order));
> + link_order.next = NULL;
> + link_order.type = bfd_indirect_link_order;
> + link_order.offset = 0;
> + link_order.size = bfd_section_size (abfd, sec);
> + link_order.u.indirect.section = sec;
> +
> + data = NULL;
> + if (outbuf == NULL)
> + {
> + data = bfd_malloc (bfd_section_size (abfd, sec));
> + outbuf = data;
> + }
> + bfd_link_add_symbols (abfd, &link_info);
> +
> + storage_needed = bfd_get_symtab_upper_bound (abfd);
> + symbol_table = (asymbol **) bfd_malloc (storage_needed);
> + number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
> +
> + contents = bfd_get_relocated_section_contents (abfd,
> + &link_info,
> + &link_order,
> + outbuf,
> + 0,
> + symbol_table);
> + if (contents == NULL && data != NULL)
> + free (data);
> +
> + /* Foul hack to prevent bfd_section_size aborts. This flag only controls
> + that macro (and the related size macros), selecting between _raw_size
> + and _cooked_size. Debug sections won't change size while we're only
> + relocating. There may be trouble here someday if it tries to run
> + relaxation unexpectedly, so make sure. */
> + BFD_ASSERT (sec->_raw_size == sec->_cooked_size);
> + sec->reloc_done = 0;
> +
> + bfd_link_hash_table_free (abfd, link_info.hash);
> +
> + return contents;
> +}
> Index: bfd/doc/Makefile.am
> ===================================================================
> RCS file: /cvs/src/src/bfd/doc/Makefile.am,v
> retrieving revision 1.8
> diff -u -p -r1.8 Makefile.am
> --- bfd/doc/Makefile.am 8 Jun 2002 08:50:44 -0000 1.8
> +++ bfd/doc/Makefile.am 27 Sep 2002 19:56:28 -0000
> @@ -248,6 +248,8 @@ BFD_H_DEP = \
> $(srcdir)/../corefile.c \
> $(srcdir)/../targets.c \
> $(srcdir)/../format.c \
> + $(srcdir)/../linker.c \
> + $(srcdir)/../simple.c \
> $(srcdir)/header.sed \
> $(srcdir)/proto.str \
> $(srcdir)/../version.h \
> Index: bfd/doc/Makefile.in
> ===================================================================
> RCS file: /cvs/src/src/bfd/doc/Makefile.in,v
> retrieving revision 1.39
> diff -u -p -r1.39 Makefile.in
> --- bfd/doc/Makefile.in 9 Aug 2002 15:38:23 -0000 1.39
> +++ bfd/doc/Makefile.in 27 Sep 2002 19:56:28 -0000
> @@ -1,6 +1,6 @@
> -# Makefile.in generated automatically by automake 1.4-p5 from Makefile.am
> +# Makefile.in generated automatically by automake 1.4 from Makefile.am
>
> -# Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc.
> +# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
> # This Makefile.in is free software; the Free Software Foundation
> # gives unlimited permission to copy and/or distribute it,
> # with or without modifications, as long as this notice is preserved.
> @@ -216,6 +216,8 @@ BFD_H_DEP = \
> $(srcdir)/../corefile.c \
> $(srcdir)/../targets.c \
> $(srcdir)/../format.c \
> + $(srcdir)/../linker.c \
> + $(srcdir)/../simple.c \
> $(srcdir)/header.sed \
> $(srcdir)/proto.str \
> $(srcdir)/../version.h \
> @@ -244,7 +246,7 @@ DIST_COMMON = ChangeLog Makefile.am Mak
>
> DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
>
> -TAR = gtar
> +TAR = tar
> GZIP_ENV = --best
> all: all-redirect
> .SUFFIXES:
> @@ -330,7 +332,7 @@ uninstall-info:
> else ii=; fi; \
> list='$(INFO_DEPS)'; \
> for file in $$list; do \
> - test -z "$$ii" \
> + test -z "$ii" \
> || install-info --info-dir=$(DESTDIR)$(infodir) --remove $$file; \
> done
> @$(NORMAL_UNINSTALL)
>
--
Daniel Jacobowitz
MontaVista Software Debian GNU/Linux Developer