Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | From 27f155fd0ab4ba954366643dbfdc3c7104933794 Mon Sep 17 00:00:00 2001 |
| 2 | From: Sandra Loosemore <sandra@codesourcery.com> |
| 3 | Date: Sun, 27 Dec 2015 12:30:26 -0800 |
| 4 | Subject: [PATCH 14/14] Correct nios2 _gp address computation. |
| 5 | |
| 6 | 2015-12-27 Sandra Loosemore <sandra@codesourcery.com> |
| 7 | |
| 8 | bfd/ |
| 9 | * elf32-nios2.c (nios2_elf_assign_gp): Correct computation of _gp |
| 10 | address. |
| 11 | (nios2_elf32_relocate_section): Tidy code for R_NIOS2_GPREL error |
| 12 | messages. |
| 13 | |
| 14 | Signed-off-by: Marek Vasut <marex@denx.de> |
| 15 | Upstream-Status: Backport |
| 16 | --- |
| 17 | bfd/elf32-nios2.c | 31 +++++++++++++++++++++---------- |
| 18 | 1 file changed, 21 insertions(+), 10 deletions(-) |
| 19 | |
| 20 | diff --git a/bfd/elf32-nios2.c b/bfd/elf32-nios2.c |
| 21 | index 1c54320..babecf3 100644 |
| 22 | --- a/bfd/elf32-nios2.c |
| 23 | +++ b/bfd/elf32-nios2.c |
| 24 | @@ -3086,7 +3086,15 @@ lookup: |
| 25 | case bfd_link_hash_defined: |
| 26 | case bfd_link_hash_defweak: |
| 27 | gp_found = TRUE; |
| 28 | - *pgp = lh->u.def.value; |
| 29 | + { |
| 30 | + asection *sym_sec = lh->u.def.section; |
| 31 | + bfd_vma sym_value = lh->u.def.value; |
| 32 | + |
| 33 | + if (sym_sec->output_section) |
| 34 | + sym_value = (sym_value + sym_sec->output_offset |
| 35 | + + sym_sec->output_section->vma); |
| 36 | + *pgp = sym_value; |
| 37 | + } |
| 38 | break; |
| 39 | case bfd_link_hash_indirect: |
| 40 | case bfd_link_hash_warning: |
| 41 | @@ -3719,7 +3727,6 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 42 | struct elf32_nios2_link_hash_entry *eh; |
| 43 | bfd_vma relocation; |
| 44 | bfd_vma gp; |
| 45 | - bfd_vma reloc_address; |
| 46 | bfd_reloc_status_type r = bfd_reloc_ok; |
| 47 | const char *name = NULL; |
| 48 | int r_type; |
| 49 | @@ -3762,12 +3769,6 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 50 | if (bfd_link_relocatable (info)) |
| 51 | continue; |
| 52 | |
| 53 | - if (sec && sec->output_section) |
| 54 | - reloc_address = (sec->output_section->vma + sec->output_offset |
| 55 | - + rel->r_offset); |
| 56 | - else |
| 57 | - reloc_address = 0; |
| 58 | - |
| 59 | if (howto) |
| 60 | { |
| 61 | switch (howto->type) |
| 62 | @@ -3816,6 +3817,15 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 63 | /* Turns an absolute address into a gp-relative address. */ |
| 64 | if (!nios2_elf_assign_gp (output_bfd, &gp, info)) |
| 65 | { |
| 66 | + bfd_vma reloc_address; |
| 67 | + |
| 68 | + if (sec && sec->output_section) |
| 69 | + reloc_address = (sec->output_section->vma |
| 70 | + + sec->output_offset |
| 71 | + + rel->r_offset); |
| 72 | + else |
| 73 | + reloc_address = 0; |
| 74 | + |
| 75 | format = _("global pointer relative relocation at address " |
| 76 | "0x%08x when _gp not defined\n"); |
| 77 | sprintf (msgbuf, format, reloc_address); |
| 78 | @@ -3825,7 +3835,7 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 79 | else |
| 80 | { |
| 81 | bfd_vma symbol_address = rel->r_addend + relocation; |
| 82 | - relocation = relocation + rel->r_addend - gp; |
| 83 | + relocation = symbol_address - gp; |
| 84 | rel->r_addend = 0; |
| 85 | if (((signed) relocation < -32768 |
| 86 | || (signed) relocation > 32767) |
| 87 | @@ -3833,6 +3843,8 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 88 | || h->root.type == bfd_link_hash_defined |
| 89 | || h->root.type == bfd_link_hash_defweak)) |
| 90 | { |
| 91 | + if (h) |
| 92 | + name = h->root.root.string; |
| 93 | format = _("Unable to reach %s (at 0x%08x) from the " |
| 94 | "global pointer (at 0x%08x) because the " |
| 95 | "offset (%d) is out of the allowed range, " |
| 96 | @@ -3848,7 +3860,6 @@ nios2_elf32_relocate_section (bfd *output_bfd, |
| 97 | rel->r_offset, relocation, |
| 98 | rel->r_addend); |
| 99 | } |
| 100 | - |
| 101 | break; |
| 102 | case R_NIOS2_UJMP: |
| 103 | r = nios2_elf32_do_ujmp_relocate (input_bfd, howto, |
| 104 | -- |
| 105 | 2.7.1 |
| 106 | |