This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Elf_Internal_Shdr field tweaks
- From: Alan Modra <amodra at bigpond dot net dot au>
- To: binutils at sourceware dot org
- Date: Thu, 13 Mar 2008 16:00:21 +1030
- Subject: Elf_Internal_Shdr field tweaks
This fixes a small problem I noticed yesterday. sh_addalign in a
64-bit section header is 8 bytes, but our internal form only uses an
unsigned int for the field. Also, sh_info and sh_link are only 4
bytes so there is no need for their internal versions to be unsigned
long.
include/elf/
* internal.h (Elf_Internal_Shdr): Change sh_link and sh_info from
unsigned long to unsigned int. Change sh_addralign to bfd_vma.
Order struct as for external version.
bfd/
* elf.c (_bfd_elf_make_section_from_shdr): Remove unnecessary cast.
(_bfd_elf_assign_file_position_for_section): Simplify align.
(_bfd_elf_init_reloc_shdr): Ensure shift expression wide enough
for sh_addralign.
(elf_fake_sections, swap_out_syms): Likewise.
* elflink.c (bfd_elf_final_link): Likewise.
binutils/
* readelf.c: Use %u throughout when printing sh_link or sh_info,
%lu when printing sh_addralign.
(process_version_sections): Use identical formats when printing
all offset and sh_link fields.
Index: include/elf/internal.h
===================================================================
RCS file: /cvs/src/src/include/elf/internal.h,v
retrieving revision 1.20
diff -u -p -r1.20 internal.h
--- include/elf/internal.h 12 Mar 2008 08:36:58 -0000 1.20
+++ include/elf/internal.h 13 Mar 2008 02:37:37 -0000
@@ -105,12 +105,12 @@ typedef struct elf_internal_shdr {
unsigned int sh_type; /* Type of section */
bfd_vma sh_flags; /* Miscellaneous section attributes */
bfd_vma sh_addr; /* Section virtual addr at execution */
+ file_ptr sh_offset; /* Section file offset */
bfd_size_type sh_size; /* Size of section in bytes */
+ unsigned int sh_link; /* Index of another section */
+ unsigned int sh_info; /* Additional section information */
+ bfd_vma sh_addralign; /* Section alignment */
bfd_size_type sh_entsize; /* Entry size if section holds table */
- unsigned long sh_link; /* Index of another section */
- unsigned long sh_info; /* Additional section information */
- file_ptr sh_offset; /* Section file offset */
- unsigned int sh_addralign; /* Section alignment */
/* The internal rep also has some cached info associated with it. */
asection * bfd_section; /* Associated BFD section. */
Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.438
diff -u -p -r1.438 elf.c
--- bfd/elf.c 12 Mar 2008 08:36:58 -0000 1.438
+++ bfd/elf.c 13 Mar 2008 02:37:07 -0000
@@ -828,7 +828,7 @@ _bfd_elf_make_section_from_shdr (bfd *ab
if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
|| ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
|| ! bfd_set_section_alignment (abfd, newsect,
- bfd_log2 ((bfd_vma) hdr->sh_addralign)))
+ bfd_log2 (hdr->sh_addralign)))
return FALSE;
flags = SEC_NO_FLAGS;
@@ -2449,7 +2449,7 @@ _bfd_elf_init_reloc_shdr (bfd *abfd,
rel_hdr->sh_entsize = (use_rela_p
? bed->s->sizeof_rela
: bed->s->sizeof_rel);
- rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
+ rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
rel_hdr->sh_flags = 0;
rel_hdr->sh_addr = 0;
rel_hdr->sh_size = 0;
@@ -2496,7 +2496,7 @@ elf_fake_sections (bfd *abfd, asection *
this_hdr->sh_offset = 0;
this_hdr->sh_size = asect->size;
this_hdr->sh_link = 0;
- this_hdr->sh_addralign = 1 << asect->alignment_power;
+ this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
/* The sh_entsize and sh_info fields may have been set already by
copy_private_section_data. */
@@ -3221,14 +3221,8 @@ _bfd_elf_assign_file_position_for_sectio
file_ptr offset,
bfd_boolean align)
{
- if (align)
- {
- unsigned int al;
-
- al = i_shdrp->sh_addralign;
- if (al > 1)
- offset = BFD_ALIGN (offset, al);
- }
+ if (align && i_shdrp->sh_addralign > 1)
+ offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
i_shdrp->sh_offset = offset;
if (i_shdrp->bfd_section != NULL)
i_shdrp->bfd_section->filepos = offset;
@@ -6145,7 +6139,7 @@ swap_out_syms (bfd *abfd,
symtab_hdr->sh_entsize = bed->s->sizeof_sym;
symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
- symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
+ symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
symstrtab_hdr->sh_type = SHT_STRTAB;
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.301
diff -u -p -r1.301 elflink.c
--- bfd/elflink.c 12 Mar 2008 08:36:58 -0000 1.301
+++ bfd/elflink.c 13 Mar 2008 02:37:14 -0000
@@ -10166,7 +10166,7 @@ bfd_elf_final_link (bfd *abfd, struct bf
/* sh_link is set in assign_section_numbers. */
/* sh_info is set below. */
/* sh_offset is set just below. */
- symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
+ symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
off = elf_tdata (abfd)->next_file_pos;
off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
Index: binutils/readelf.c
===================================================================
RCS file: /cvs/src/src/binutils/readelf.c,v
retrieving revision 1.402
diff -u -p -r1.402 readelf.c
--- binutils/readelf.c 12 Mar 2008 08:36:59 -0000 1.402
+++ binutils/readelf.c 13 Mar 2008 02:37:20 -0000
@@ -3340,7 +3340,7 @@ process_file_header (void)
(long) elf_header.e_shstrndx);
if (section_headers != NULL
&& elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
- printf (" (%ld)", (long) section_headers[0].sh_link);
+ printf (" (%u)", section_headers[0].sh_link);
else if (elf_header.e_shstrndx >= elf_header.e_shnum)
printf (" <corrupt: out of range>");
putc ('\n', stdout);
@@ -4413,9 +4413,9 @@ process_section_headers (FILE *file)
else
printf (" %3s ", get_elf_section_flags (section->sh_flags));
- printf ("%2ld %3lu %2ld\n",
- (unsigned long) section->sh_link,
- (unsigned long) section->sh_info,
+ printf ("%2u %3u %2lu\n",
+ section->sh_link,
+ section->sh_info,
(unsigned long) section->sh_addralign);
}
else if (do_wide)
@@ -4451,12 +4451,10 @@ process_section_headers (FILE *file)
else
printf (" %3s ", get_elf_section_flags (section->sh_flags));
- printf ("%2ld %3lu ",
- (unsigned long) section->sh_link,
- (unsigned long) section->sh_info);
+ printf ("%2u %3u ", section->sh_link, section->sh_info);
if ((unsigned long) section->sh_addralign == section->sh_addralign)
- printf ("%2ld\n", (unsigned long) section->sh_addralign);
+ printf ("%2lu\n", (unsigned long) section->sh_addralign);
else
{
print_vma (section->sh_addralign, DEC);
@@ -4475,13 +4473,13 @@ process_section_headers (FILE *file)
printf (" ");
print_vma (section->sh_offset, LONG_HEX);
}
- printf (" %ld\n ", (unsigned long) section->sh_link);
+ printf (" %u\n ", section->sh_link);
print_vma (section->sh_size, LONG_HEX);
putchar (' ');
print_vma (section->sh_entsize, LONG_HEX);
- printf (" %-16lu %ld\n",
- (unsigned long) section->sh_info,
+ printf (" %-16u %lu\n",
+ section->sh_info,
(unsigned long) section->sh_addralign);
}
else
@@ -4502,9 +4500,9 @@ process_section_headers (FILE *file)
printf (" %3s ", get_elf_section_flags (section->sh_flags));
- printf (" %2ld %3lu %ld\n",
- (unsigned long) section->sh_link,
- (unsigned long) section->sh_info,
+ printf (" %2u %3u %lu\n",
+ section->sh_link,
+ section->sh_info,
(unsigned long) section->sh_addralign);
}
@@ -6559,12 +6557,12 @@ process_version_sections (FILE *file)
found = 1;
printf
- (_("\nVersion definition section '%s' contains %ld entries:\n"),
+ (_("\nVersion definition section '%s' contains %u entries:\n"),
SECTION_NAME (section), section->sh_info);
printf (_(" Addr: 0x"));
printf_vma (section->sh_addr);
- printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
+ printf (_(" Offset: %#08lx Link: %u (%s)\n"),
(unsigned long) section->sh_offset, section->sh_link,
section->sh_link < elf_header.e_shnum
? SECTION_NAME (section_headers + section->sh_link)
@@ -6661,12 +6659,12 @@ process_version_sections (FILE *file)
found = 1;
- printf (_("\nVersion needs section '%s' contains %ld entries:\n"),
+ printf (_("\nVersion needs section '%s' contains %u entries:\n"),
SECTION_NAME (section), section->sh_info);
printf (_(" Addr: 0x"));
printf_vma (section->sh_addr);
- printf (_(" Offset: %#08lx Link to section: %ld (%s)\n"),
+ printf (_(" Offset: %#08lx Link: %u (%s)\n"),
(unsigned long) section->sh_offset, section->sh_link,
section->sh_link < elf_header.e_shnum
? SECTION_NAME (section_headers + section->sh_link)
@@ -6787,7 +6785,7 @@ process_version_sections (FILE *file)
printf (_(" Addr: "));
printf_vma (section->sh_addr);
- printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
+ printf (_(" Offset: %#08lx Link: %u (%s)\n"),
(unsigned long) section->sh_offset, section->sh_link,
SECTION_NAME (link_section));
--
Alan Modra
Australia Development Lab, IBM