aboutsummaryrefslogtreecommitdiff
path: root/bfd
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2017-11-24 06:41:40 -0800
committerH.J. Lu <hjl.tools@gmail.com>2017-11-24 06:41:52 -0800
commit650444eb540f9fd85e821567a3f943b4bc41b8be (patch)
tree2c5e0bf7934a74df34cfd3a0c9377d62903717bb /bfd
parenta63f2d2feedcfce401ae1d7d03d119bfa5e4d8bc (diff)
downloadgdb-650444eb540f9fd85e821567a3f943b4bc41b8be.zip
gdb-650444eb540f9fd85e821567a3f943b4bc41b8be.tar.gz
gdb-650444eb540f9fd85e821567a3f943b4bc41b8be.tar.bz2
elf: Properly compute offsets of note descriptor and next note
According to gABI, in a note entry, the note name field, not note name size, is padded for the note descriptor. And the note descriptor field, not note descriptor size, is padded for the next note entry. Also notes are aligned to 4 bytes in 32-bit objects and 8 bytes in 64-bit objects. Since on Linux, .note.ABI-tag and .note.gnu.build-id notes are always aligned to 4 bytes, we need to use alignment of note section or note segment, instead of assuming alignment based on ELF file class. Tested on i686 and x86-64. bfd/ PR binutils/22444 * elf.c (elf_read_notes): Add an argument for note aligment. (elf_parse_notes): Likewise. (_bfd_elf_make_section_from_shdr): Pass section aligment to elf_parse_notes. (bfd_section_from_phdr): Pass segment aligment to elf_read_notes. (elf_parse_notes): Add an argument for note aligment. Use ELF_NOTE_DESC_OFFSET to get the offset of the note descriptor. Use ELF_NOTE_NEXT_OFFSET to get the offset of the next note entry. (elf_read_notes): Add an argument for note aligment and pass it to elf_parse_notes. binutils/ PR binutils/22444 * readelf.c (process_notes_at): Use ELF_NOTE_DESC_OFFSET to get the offset of the note descriptor. Use ELF_NOTE_NEXT_OFFSET to get the offset of the next note entry. include/ PR binutils/22444 * elf/external.h (ELF_ALIGN_UP): New. (ELF_NOTE_DESC_OFFSET): Likewise. (ELF_NOTE_NEXT_OFFSET): Likewise.
Diffstat (limited to 'bfd')
-rw-r--r--bfd/ChangeLog15
-rw-r--r--bfd/elf.c24
2 files changed, 29 insertions, 10 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 55fe3b4..0395dcf 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,18 @@
+2017-11-24 H.J. Lu <hongjiu.lu@intel.com>
+
+ PR binutils/22444
+ * elf.c (elf_read_notes): Add an argument for note aligment.
+ (elf_parse_notes): Likewise.
+ (_bfd_elf_make_section_from_shdr): Pass section aligment to
+ elf_parse_notes.
+ (bfd_section_from_phdr): Pass segment aligment to elf_read_notes.
+ (elf_parse_notes): Add an argument for note aligment. Use
+ ELF_NOTE_DESC_OFFSET to get the offset of the note descriptor.
+ Use ELF_NOTE_NEXT_OFFSET to get the offset of the next note
+ entry.
+ (elf_read_notes): Add an argument for note aligment and pass it
+ to elf_parse_notes.
+
2017-11-23 Alan Modra <amodra@gmail.com>
* elf32-hppa.c (pc_dynrelocs): Define.
diff --git a/bfd/elf.c b/bfd/elf.c
index 8cd67ad..d42c949 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -52,9 +52,10 @@ static int elf_sort_sections (const void *, const void *);
static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
static bfd_boolean prep_headers (bfd *);
static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
-static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
+static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
+ size_t align) ;
static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
- file_ptr offset);
+ file_ptr offset, size_t align);
/* Swap version information in and out. The version information is
currently size independent. If that ever changes, this code will
@@ -1089,7 +1090,8 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
return FALSE;
- elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset);
+ elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
+ hdr->sh_offset, hdr->sh_addralign);
free (contents);
}
@@ -2990,7 +2992,8 @@ bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
case PT_NOTE:
if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
return FALSE;
- if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
+ if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
+ hdr->p_align))
return FALSE;
return TRUE;
@@ -10970,14 +10973,14 @@ elfcore_write_register_note (bfd *abfd,
}
static bfd_boolean
-elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
+elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
+ size_t align)
{
char *p;
p = buf;
while (p < buf + size)
{
- /* FIXME: bad alignment assumption. */
Elf_External_Note *xnp = (Elf_External_Note *) p;
Elf_Internal_Note in;
@@ -10992,7 +10995,7 @@ elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
return FALSE;
in.descsz = H_GET_32 (abfd, xnp->descsz);
- in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
+ in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
in.descpos = offset + (in.descdata - buf);
if (in.descsz != 0
&& (in.descdata >= buf + size
@@ -11054,14 +11057,15 @@ elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
break;
}
- p = in.descdata + BFD_ALIGN (in.descsz, 4);
+ p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
}
return TRUE;
}
static bfd_boolean
-elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
+elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
+ size_t align)
{
char *buf;
@@ -11080,7 +11084,7 @@ elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
buf[size] = 0;
if (bfd_bread (buf, size, abfd) != size
- || !elf_parse_notes (abfd, buf, size, offset))
+ || !elf_parse_notes (abfd, buf, size, offset, align))
{
free (buf);
return FALSE;