From 37b01f6a13f21f274bf9758ecbf5d9efd6153444 Mon Sep 17 00:00:00 2001 From: Dan Gissel Date: Fri, 11 Mar 2016 09:17:28 +0000 Subject: [PATCH] Fix some places where octet to byte conversions are needed. PR 19713 * elf.c (_bfd_elf_section_offset): Ensure that the returned offset uses bytes not octets. * elflink.c (resolve_section): Likewise. Add a bfd parameter. (eval_section): Pass the input_bfd to resolve_section. (bfd_elf_perform_complex_relocation): Convert byte offset to octets before read and writing values. (elf_link_input_bfd): Add byte to octet conversions. (elf_reloc_link_order): Likewise. (elf_fixup_link_order): Likewise. (bfd_elf_final_link): Likewise. * reloc.c (_bfd_final_link_relocate): Likewise. * syms.c (_bfd_stab_section_find_nearest_line): Likewise. --- bfd/ChangeLog | 17 +++++++++++++++++ bfd/elf.c | 13 ++++++++++++- bfd/elflink.c | 38 ++++++++++++++++++++++++-------------- bfd/reloc.c | 3 ++- bfd/section.c | 2 +- bfd/syms.c | 6 ++++-- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index dd4ec7556c..b94296e1cd 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,20 @@ +2016-03-11 Dan Gissel + + PR 19713 + * elf.c (_bfd_elf_section_offset): Ensure that the returned offset + uses bytes not octets. + * elflink.c (resolve_section): Likewise. + Add a bfd parameter. + (eval_section): Pass the input_bfd to resolve_section. + (bfd_elf_perform_complex_relocation): Convert byte offset to + octets before read and writing values. + (elf_link_input_bfd): Add byte to octet conversions. + (elf_reloc_link_order): Likewise. + (elf_fixup_link_order): Likewise. + (bfd_elf_final_link): Likewise. + * reloc.c (_bfd_final_link_relocate): Likewise. + * syms.c (_bfd_stab_section_find_nearest_line): Likewise. + 2016-03-10 Nick Clifton * config.bfd: Mark the i370 target as obsolete. diff --git a/bfd/elf.c b/bfd/elf.c index 1013644b95..90319a2702 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -10464,6 +10464,12 @@ _bfd_elf_rel_local_sym (bfd *abfd, sym->st_value + addend); } +/* Adjust an address within a section. Given OFFSET within SEC, return + the new offset within the section, based upon changes made to the + section. Returns -1 if the offset is now invalid. + The offset (in abnd out) is in target sized bytes, however big a + byte may be. */ + bfd_vma _bfd_elf_section_offset (bfd *abfd, struct bfd_link_info *info, @@ -10477,12 +10483,17 @@ _bfd_elf_section_offset (bfd *abfd, offset); case SEC_INFO_TYPE_EH_FRAME: return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); + default: if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) { + /* Reverse the offset. */ const struct elf_backend_data *bed = get_elf_backend_data (abfd); bfd_size_type address_size = bed->s->arch_size / 8; - offset = sec->size - offset - address_size; + + /* address_size and sec->size are in octets. Convert + to bytes before subtracting the original offset. */ + offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; } return offset; } diff --git a/bfd/elflink.c b/bfd/elflink.c index 991efacfe1..c2ad11bfbb 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -7751,10 +7751,15 @@ resolve_symbol (const char *name, return FALSE; } +/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in + bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section + names like "foo.end" which is the end address of section "foo". */ + static bfd_boolean resolve_section (const char *name, asection *sections, - bfd_vma *result) + bfd_vma *result, + bfd * abfd) { asection *curr; unsigned int len; @@ -7767,6 +7772,7 @@ resolve_section (const char *name, } /* Hmm. still haven't found it. try pseudo-section names. */ + /* FIXME: This could be coded more efficiently... */ for (curr = sections; curr; curr = curr->next) { len = strlen (curr->name); @@ -7777,7 +7783,7 @@ resolve_section (const char *name, { if (strncmp (".end", name + len, 4) == 0) { - *result = curr->vma + curr->size; + *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); return TRUE; } @@ -7859,7 +7865,7 @@ eval_symbol (bfd_vma *result, if (symbol_is_section) { - if (!resolve_section (symbuf, flinfo->output_bfd->sections, result) + if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) && !resolve_symbol (symbuf, input_bfd, flinfo, result, isymbuf, locsymcount)) { @@ -7872,7 +7878,7 @@ eval_symbol (bfd_vma *result, if (!resolve_symbol (symbuf, input_bfd, flinfo, result, isymbuf, locsymcount) && !resolve_section (symbuf, flinfo->output_bfd->sections, - result)) + result, input_bfd)) { undefined_reference ("symbol", symbuf); return FALSE; @@ -8096,8 +8102,8 @@ bfd_elf_perform_complex_relocation (bfd *input_bfd, else shift = (8 * wordsz) - (start + len); - /* FIXME: octets_per_byte. */ - x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); + x = get_value (wordsz, chunksz, input_bfd, + contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); #ifdef DEBUG printf ("Doing complex reloc: " @@ -8129,8 +8135,8 @@ bfd_elf_perform_complex_relocation (bfd *input_bfd, (unsigned long) relocation, (unsigned long) (mask << shift), (unsigned long) ((relocation & mask) << shift), (unsigned long) x); #endif - /* FIXME: octets_per_byte. */ - put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); + put_value (wordsz, chunksz, input_bfd, x, + contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); return r; } @@ -10535,11 +10541,13 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) break; default: { - /* FIXME: octets_per_byte. */ if (! (o->flags & SEC_EXCLUDE)) { file_ptr offset = (file_ptr) o->output_offset; bfd_size_type todo = o->size; + + offset *= bfd_octets_per_byte (output_bfd); + if ((o->flags & SEC_ELF_REVERSE_COPY)) { /* Reverse-copy input section to output. */ @@ -10703,8 +10711,11 @@ elf_reloc_link_order (bfd *output_bfd, } break; } + ok = bfd_set_section_contents (output_bfd, output_section, buf, - link_order->offset, size); + link_order->offset + * bfd_octets_per_byte (output_bfd), + size); free (buf); if (! ok) return FALSE; @@ -10886,9 +10897,8 @@ elf_fixup_link_order (bfd *abfd, asection *o) { s = sections[n]->u.indirect.section; offset &= ~(bfd_vma) 0 << s->alignment_power; - s->output_offset = offset; + s->output_offset = offset / bfd_octets_per_byte (abfd); sections[n]->offset = offset; - /* FIXME: octets_per_byte. */ offset += sections[n]->size; } @@ -11948,10 +11958,10 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) continue; if (strcmp (o->name, ".dynstr") != 0) { - /* FIXME: octets_per_byte. */ if (! bfd_set_section_contents (abfd, o->output_section, o->contents, - (file_ptr) o->output_offset, + (file_ptr) o->output_offset + * bfd_octets_per_byte (abfd), o->size)) goto error_return; } diff --git a/bfd/reloc.c b/bfd/reloc.c index a1683d8df5..0135c0436b 100644 --- a/bfd/reloc.c +++ b/bfd/reloc.c @@ -1375,7 +1375,8 @@ _bfd_final_link_relocate (reloc_howto_type *howto, } return _bfd_relocate_contents (howto, input_bfd, relocation, - contents + address); + contents + + address * bfd_octets_per_byte (input_bfd)); } /* Relocate a given location using a given value and howto. */ diff --git a/bfd/section.c b/bfd/section.c index 85dc141a1a..f0a66b247e 100644 --- a/bfd/section.c +++ b/bfd/section.c @@ -430,7 +430,7 @@ CODE_FRAGMENT . information. *} . bfd_vma lma; . -. {* The size of the section in octets, as it will be output. +. {* The size of the section in *octets*, as it will be output. . Contains a value even if the section has no contents (e.g., the . size of <<.bss>>). *} . bfd_size_type size; diff --git a/bfd/syms.c b/bfd/syms.c index 118595d3d0..5a22c0b38b 100644 --- a/bfd/syms.c +++ b/bfd/syms.c @@ -1083,11 +1083,13 @@ _bfd_stab_section_find_nearest_line (bfd *abfd, return FALSE; } - val = bfd_get_32 (abfd, info->stabs + r->address); + val = bfd_get_32 (abfd, info->stabs + + r->address * bfd_octets_per_byte (abfd)); val &= r->howto->src_mask; sym = *r->sym_ptr_ptr; val += sym->value + sym->section->vma + r->addend; - bfd_put_32 (abfd, (bfd_vma) val, info->stabs + r->address); + bfd_put_32 (abfd, (bfd_vma) val, info->stabs + + r->address * bfd_octets_per_byte (abfd)); } } -- 2.34.1