aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2019-11-18 17:09:01 +1030
committerAlan Modra <amodra@gmail.com>2019-11-18 22:06:09 +1030
commit6d6c25c8eaaf42755a759beeb2996502322b960c (patch)
tree3ec735f2069634d339bde6d46dab7a7c758eb164 /bfd/elf-nacl.c
parentPR25196, abort in rewrite_elf_program_header (diff)
downloadbinutils-gdb-6d6c25c8eaaf42755a759beeb2996502322b960c.tar.gz
binutils-gdb-6d6c25c8eaaf42755a759beeb2996502322b960c.tar.bz2
binutils-gdb-6d6c25c8eaaf42755a759beeb2996502322b960c.zip
elf_backend_modify_headers
This patch renames elf_backend_modify_program_headers and moves the elf.c code tweaking the ELF file header for -pie -Ttext-segment to a new function, _bfd_elf_modify_headers, which then becomes the default elf_backed_modify_headers and is called from any other target elf_backed_modify_headers. * elf-bfd.h (struct elf_backend_data <elf_backend_modify_headers>): Rename from elf_backend_modify_program_headers. (_bfd_elf_modify_headers): Declare. * elf.c (assign_file_positions_except_relocs): Set elf_program_header_size. Always call elf_backend_modify_headers. Extract code modifying file header.. (_bfd_elf_modify_headers): ..to here. New function. * elf32-arm.c (elf_backend_modify_headers): Renamed from elf_backend_modify_program_headers. * elf32-i386.c: Similarly. * elf64-x86-64.c: Similarly. * elfxx-target.h: Similarly. Default elf_backend_modify_headers to _bfd_elf_modify_headers. * elf-nacl.h (nacl_modify_headers): Rename from nacl_modify_program_headers. * elf-nacl.c (nacl_modify_headers): Rename from nacl_modify_program_headers and call _bfd_elf_modify_headers. * elf32-rx.c (elf32_rx_modify_headers): Similarly. * elf32-spu.c (spu_elf_modify_headers): Similarly. * elfnn-ia64.c (elfNN_ia64_modify_headers): Similarly. * elf32-sh.c (elf_backend_modify_program_headers): Don't undef.
Diffstat (limited to 'bfd/elf-nacl.c')
-rw-r--r--bfd/elf-nacl.c123
1 files changed, 63 insertions, 60 deletions
diff --git a/bfd/elf-nacl.c b/bfd/elf-nacl.c
index ddac3b372bf..d8d67f338ff 100644
--- a/bfd/elf-nacl.c
+++ b/bfd/elf-nacl.c
@@ -235,90 +235,93 @@ nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
proper order for the ELF rule that they must appear in ascending address
order. So find the two segments we swapped before, and swap them back. */
bfd_boolean
-nacl_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
+nacl_modify_headers (bfd *abfd, struct bfd_link_info *info)
{
- struct elf_segment_map **m = &elf_seg_map (abfd);
- Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
- Elf_Internal_Phdr *p = phdr;
-
if (info != NULL && info->user_phdrs)
/* The linker script used PHDRS explicitly, so don't change what the
user asked for. */
- return TRUE;
-
- /* Find the PT_LOAD that contains the headers (should be the first). */
- while (*m != NULL)
- {
- if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
- break;
-
- m = &(*m)->next;
- ++p;
- }
-
- if (*m != NULL)
+ ;
+ else
{
- struct elf_segment_map **first_load_seg = m;
- Elf_Internal_Phdr *first_load_phdr = p;
- struct elf_segment_map **next_load_seg = NULL;
- Elf_Internal_Phdr *next_load_phdr = NULL;
-
- /* Now move past that first one and find the PT_LOAD that should be
- before it by address order. */
-
- m = &(*m)->next;
- ++p;
+ struct elf_segment_map **m = &elf_seg_map (abfd);
+ Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
+ Elf_Internal_Phdr *p = phdr;
+ /* Find the PT_LOAD that contains the headers (should be the first). */
while (*m != NULL)
{
- if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
- {
- next_load_seg = m;
- next_load_phdr = p;
- break;
- }
+ if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
+ break;
m = &(*m)->next;
++p;
}
- /* Swap their positions in the segment_map back to how they used to be.
- The phdrs have already been set up by now, so we have to slide up
- the earlier ones to insert the one that should be first. */
- if (next_load_seg != NULL)
+ if (*m != NULL)
{
- Elf_Internal_Phdr move_phdr;
- struct elf_segment_map *first_seg = *first_load_seg;
- struct elf_segment_map *next_seg = *next_load_seg;
- struct elf_segment_map *first_next = first_seg->next;
- struct elf_segment_map *next_next = next_seg->next;
+ struct elf_segment_map **first_load_seg = m;
+ Elf_Internal_Phdr *first_load_phdr = p;
+ struct elf_segment_map **next_load_seg = NULL;
+ Elf_Internal_Phdr *next_load_phdr = NULL;
+
+ /* Now move past that first one and find the PT_LOAD that should be
+ before it by address order. */
- if (next_load_seg == &first_seg->next)
+ m = &(*m)->next;
+ ++p;
+
+ while (*m != NULL)
{
- *first_load_seg = next_seg;
- next_seg->next = first_seg;
- first_seg->next = next_next;
+ if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
+ {
+ next_load_seg = m;
+ next_load_phdr = p;
+ break;
+ }
+
+ m = &(*m)->next;
+ ++p;
}
- else
+
+ /* Swap their positions in the segment_map back to how they
+ used to be. The phdrs have already been set up by now,
+ so we have to slide up the earlier ones to insert the one
+ that should be first. */
+ if (next_load_seg != NULL)
{
- *first_load_seg = first_next;
- *next_load_seg = next_next;
+ Elf_Internal_Phdr move_phdr;
+ struct elf_segment_map *first_seg = *first_load_seg;
+ struct elf_segment_map *next_seg = *next_load_seg;
+ struct elf_segment_map *first_next = first_seg->next;
+ struct elf_segment_map *next_next = next_seg->next;
- first_seg->next = *next_load_seg;
- *next_load_seg = first_seg;
+ if (next_load_seg == &first_seg->next)
+ {
+ *first_load_seg = next_seg;
+ next_seg->next = first_seg;
+ first_seg->next = next_next;
+ }
+ else
+ {
+ *first_load_seg = first_next;
+ *next_load_seg = next_next;
- next_seg->next = *first_load_seg;
- *first_load_seg = next_seg;
- }
+ first_seg->next = *next_load_seg;
+ *next_load_seg = first_seg;
- move_phdr = *next_load_phdr;
- memmove (first_load_phdr + 1, first_load_phdr,
- (next_load_phdr - first_load_phdr) * sizeof move_phdr);
- *first_load_phdr = move_phdr;
+ next_seg->next = *first_load_seg;
+ *first_load_seg = next_seg;
+ }
+
+ move_phdr = *next_load_phdr;
+ memmove (first_load_phdr + 1, first_load_phdr,
+ (next_load_phdr - first_load_phdr) * sizeof move_phdr);
+ *first_load_phdr = move_phdr;
+ }
}
}
- return TRUE;
+ return _bfd_elf_modify_headers (abfd, info);
}
bfd_boolean