This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: Problem with objcopy phdrs and patch
- From: David Heine <dlheine at tensilica dot com>
- To: binutils at sources dot redhat dot com
- Date: Wed, 05 May 2004 12:06:22 -0700
- Subject: Re: Problem with objcopy phdrs and patch
- Organization: Tensilica, Inc
- References: <40993A1B.3080300@tensilica.com>
Here is the proposed patch and Changelog
binutils ChangeLog
2004-05-05 David Heine
* objcopy.c (setup_bfd_headers): New function.
(copy_object): Call setup_bfd_headers.
bfd ChangeLog
2004-05-05 David Heine
* bfd.c (bfd_copy_private_header_data): Define.
* elf-bfd.h (_bfd_elf_copy_private_header_data): Declare.
* elf.c (_bfd_elf_copy_private_section_data): Remove code to set up
segments by calling copy_private_bfd_data.\
(_bfd_elf_copy_private_header_data): Define.
* elf-xxtarget.h (bfd_elfNN_bfd_copy_private_header_data): Define.
* libbfd-in.h (_bfd_generic_bfd_copy_private_header_data): Define.
* targets.c (BFD_JUMP_TABLE_COPY): Add
_bfd_copy_private_header_data.
* bfd-in2.h: Regenerate.
* libbfd.h: Regenerate.
Index: bfd-in2.h
===================================================================
RCS file: /cvs/src/src/bfd/bfd-in2.h,v
retrieving revision 1.276
diff -u -r1.276 bfd-in2.h
--- bfd-in2.h 5 May 2004 14:33:13 -0000 1.276
+++ bfd-in2.h 5 May 2004 16:38:03 -0000
@@ -3945,6 +3945,11 @@
bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
+bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
+
+#define bfd_copy_private_header_data(ibfd, obfd) \
+ BFD_SEND (obfd, _bfd_copy_private_header_data, \
+ (ibfd, obfd))
bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
#define bfd_copy_private_bfd_data(ibfd, obfd) \
@@ -4225,6 +4230,7 @@
NAME##_bfd_merge_private_bfd_data, \
NAME##_bfd_copy_private_section_data, \
NAME##_bfd_copy_private_symbol_data, \
+ NAME##_bfd_copy_private_header_data, \
NAME##_bfd_set_private_flags, \
NAME##_bfd_print_private_bfd_data
@@ -4242,6 +4248,10 @@
to another. */
bfd_boolean (*_bfd_copy_private_symbol_data)
(bfd *, asymbol *, bfd *, asymbol *);
+ /* Called to copy BFD private header data from one object file
+ to another. */
+ bfd_boolean (*_bfd_copy_private_header_data)
+ (bfd *, bfd *);
/* Called to set private backend flags. */
bfd_boolean (*_bfd_set_private_flags) (bfd *, flagword);
Index: bfd.c
===================================================================
RCS file: /cvs/src/src/bfd/bfd.c,v
retrieving revision 1.61
diff -u -r1.61 bfd.c
--- bfd.c 30 Apr 2004 14:23:39 -0000 1.61
+++ bfd.c 5 May 2004 16:38:03 -0000
@@ -982,6 +982,29 @@
/*
FUNCTION
+ bfd_copy_private_header_data
+
+SYNOPSIS
+ bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
+
+DESCRIPTION
+ Copy private BFD header information from the BFD @var{ibfd} to the
+ the BFD @var{obfd}. This copies information that may require
+ sections to exist, but does not require symbol tables. Return
+ <<true>> on success, <<false>> on error.
+ Possible error returns are:
+
+ o <<bfd_error_no_memory>> -
+ Not enough memory exists to create private data for @var{obfd}.
+
+.#define bfd_copy_private_header_data(ibfd, obfd) \
+. BFD_SEND (obfd, _bfd_copy_private_header_data, \
+. (ibfd, obfd))
+
+*/
+
+/*
+FUNCTION
bfd_copy_private_bfd_data
SYNOPSIS
Index: elf-bfd.h
===================================================================
RCS file: /cvs/src/src/bfd/elf-bfd.h,v
retrieving revision 1.139
diff -u -r1.139 elf-bfd.h
--- elf-bfd.h 30 Apr 2004 14:23:39 -0000 1.139
+++ elf-bfd.h 5 May 2004 16:38:03 -0000
@@ -1376,6 +1376,8 @@
(bfd *, asection *, void *);
extern void _bfd_elf_link_just_syms
(asection *, struct bfd_link_info *);
+extern bfd_boolean _bfd_elf_copy_private_header_data
+ (bfd *, bfd *);
extern bfd_boolean _bfd_elf_copy_private_symbol_data
(bfd *, asymbol *, bfd *, asymbol *);
extern bfd_boolean _bfd_elf_copy_private_section_data
Index: elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.224
diff -u -r1.224 elf.c
--- elf.c 30 Apr 2004 15:04:30 -0000 1.224
+++ elf.c 5 May 2004 16:38:03 -0000
@@ -5234,24 +5234,6 @@
|| obfd->xvec->flavour != bfd_target_elf_flavour)
return TRUE;
- if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
- {
- asection *s;
-
- /* Only set up the segments if there are no more SEC_ALLOC
- sections. FIXME: This won't do the right thing if objcopy is
- used to remove the last SEC_ALLOC section, since objcopy
- won't call this routine in that case. */
- for (s = isec->next; s != NULL; s = s->next)
- if ((s->flags & SEC_ALLOC) != 0)
- break;
- if (s == NULL)
- {
- if (! copy_private_bfd_data (ibfd, obfd))
- return FALSE;
- }
- }
-
ihdr = &elf_section_data (isec)->this_hdr;
ohdr = &elf_section_data (osec)->this_hdr;
@@ -5270,6 +5252,29 @@
elf_group_name (osec) = elf_group_name (isec);
osec->use_rela_p = isec->use_rela_p;
+
+ return TRUE;
+}
+
+/* Copy private header information. */
+
+bfd_boolean
+_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
+{
+ if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
+ || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
+ return TRUE;
+
+ /* Copy over private BFD data if it has not already been copied.
+ This must be done here, rather than in the copy_private_bfd_data
+ entry point, because the latter is called after the section
+ contents have been set, which means that the program headers have
+ already been worked out. */
+ if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
+ {
+ if (! copy_private_bfd_data (ibfd, obfd))
+ return FALSE;
+ }
return TRUE;
}
Index: elfxx-target.h
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-target.h,v
retrieving revision 1.64
diff -u -r1.64 elfxx-target.h
--- elfxx-target.h 30 Apr 2004 14:23:39 -0000 1.64
+++ elfxx-target.h 5 May 2004 16:38:03 -0000
@@ -158,6 +158,10 @@
#define bfd_elfNN_bfd_copy_private_section_data \
_bfd_elf_copy_private_section_data
#endif
+#ifndef bfd_elfNN_bfd_copy_private_header_data
+#define bfd_elfNN_bfd_copy_private_header_data \
+ _bfd_elf_copy_private_header_data
+#endif
#ifndef bfd_elfNN_bfd_copy_private_bfd_data
#define bfd_elfNN_bfd_copy_private_bfd_data \
_bfd_elf_copy_private_bfd_data
Index: libbfd-in.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd-in.h,v
retrieving revision 1.33
diff -u -r1.33 libbfd-in.h
--- libbfd-in.h 30 Apr 2004 14:23:39 -0000 1.33
+++ libbfd-in.h 5 May 2004 16:38:03 -0000
@@ -212,6 +212,8 @@
((bfd_boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true)
#define _bfd_generic_bfd_copy_private_symbol_data \
((bfd_boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true)
+#define _bfd_generic_bfd_copy_private_header_data \
+ ((bfd_boolean (*) (bfd *, bfd *)) bfd_true)
#define _bfd_generic_bfd_print_private_bfd_data \
((bfd_boolean (*) (bfd *, void *)) bfd_true)
Index: libbfd.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd.h,v
retrieving revision 1.108
diff -u -r1.108 libbfd.h
--- libbfd.h 30 Apr 2004 14:23:40 -0000 1.108
+++ libbfd.h 5 May 2004 16:38:03 -0000
@@ -217,6 +217,8 @@
((bfd_boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true)
#define _bfd_generic_bfd_copy_private_symbol_data \
((bfd_boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true)
+#define _bfd_generic_bfd_copy_private_header_data \
+ ((bfd_boolean (*) (bfd *, bfd *)) bfd_true)
#define _bfd_generic_bfd_print_private_bfd_data \
((bfd_boolean (*) (bfd *, void *)) bfd_true)
@@ -696,9 +698,9 @@
extern bfd *bfd_last_cache;
#define bfd_cache_lookup(x) \
- ((x)==bfd_last_cache? \
- (FILE*) (bfd_last_cache->iostream): \
- bfd_cache_lookup_worker(x))
+ ((x) == bfd_last_cache ? \
+ (FILE *) (bfd_last_cache->iostream): \
+ bfd_cache_lookup_worker (x))
bfd_boolean bfd_cache_init (bfd *abfd);
bfd_boolean bfd_cache_close (bfd *abfd);
Index: targets.c
===================================================================
RCS file: /cvs/src/src/bfd/targets.c,v
retrieving revision 1.108
diff -u -r1.108 targets.c
--- targets.c 30 Apr 2004 14:23:39 -0000 1.108
+++ targets.c 5 May 2004 16:38:03 -0000
@@ -265,6 +265,7 @@
. NAME##_bfd_merge_private_bfd_data, \
. NAME##_bfd_copy_private_section_data, \
. NAME##_bfd_copy_private_symbol_data, \
+. NAME##_bfd_copy_private_header_data, \
. NAME##_bfd_set_private_flags, \
. NAME##_bfd_print_private_bfd_data
.
@@ -282,6 +283,10 @@
. to another. *}
. bfd_boolean (*_bfd_copy_private_symbol_data)
. (bfd *, asymbol *, bfd *, asymbol *);
+. {* Called to copy BFD private header data from one object file
+. to another. *}
+. bfd_boolean (*_bfd_copy_private_header_data)
+. (bfd *, bfd *);
. {* Called to set private backend flags. *}
. bfd_boolean (*_bfd_set_private_flags) (bfd *, flagword);
.
Index: objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.67
diff -u -r1.67 objcopy.c
--- objcopy.c 27 Apr 2004 06:31:16 -0000 1.67
+++ objcopy.c 5 May 2004 16:30:09 -0000
@@ -377,6 +377,7 @@
/* Forward declarations. */
static void setup_section (bfd *, asection *, void *);
+static void setup_bfd_headers (bfd *, bfd *);
static void copy_section (bfd *, asection *, void *);
static void get_sections (bfd *, asection *, void *);
static int compare_section_lma (const void *, const void *);
@@ -1178,6 +1179,8 @@
any output is done. Thus, we traverse all sections multiple times. */
bfd_map_over_sections (ibfd, setup_section, obfd);
+ setup_bfd_headers (ibfd, obfd);
+
if (add_sections != NULL)
{
struct section_add *padd;
@@ -1806,6 +1809,32 @@
}
return old_name;
+}
+
+/* Once each of the sections is copied, we may still need to do some
+ finalization work for private section headers. Do that here. */
+
+static void
+setup_bfd_headers (bfd *ibfd, bfd *obfd)
+{
+ const char *err;
+
+ /* Allow the BFD backend to copy any private data it understands
+ from the input section to the output section. */
+ if (! bfd_copy_private_header_data (ibfd, obfd))
+ {
+ err = _("private header data");
+ goto loser;
+ }
+
+ /* All went well. */
+ return;
+
+loser:
+ non_fatal (_("%s: error in %s: %s"),
+ bfd_get_filename (ibfd),
+ err, bfd_errmsg (bfd_get_error ()));
+ status = 1;
}
/* Create a section in OBFD with the same