| 1 | /* 32-bit ELF for the WebAssembly target |
| 2 | Copyright (C) 2017 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of BFD, the Binary File Descriptor library. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 19 | Boston, MA 02110-1301, USA. */ |
| 20 | |
| 21 | #include "sysdep.h" |
| 22 | #include "bfd.h" |
| 23 | #include "libbfd.h" |
| 24 | #include "elf-bfd.h" |
| 25 | #include "bfd_stdint.h" |
| 26 | #include "libiberty.h" |
| 27 | #include "elf/wasm32.h" |
| 28 | |
| 29 | static reloc_howto_type elf32_wasm32_howto_table[] = |
| 30 | { |
| 31 | HOWTO (R_WASM32_NONE, /* type */ |
| 32 | 0, /* rightshift */ |
| 33 | 3, /* size (0 = byte, 1 = short, 2 = long) */ |
| 34 | 0, /* bitsize */ |
| 35 | FALSE, /* pc_relative */ |
| 36 | 0, /* bitpos */ |
| 37 | complain_overflow_dont,/* complain_on_overflow */ |
| 38 | bfd_elf_generic_reloc, /* special_function */ |
| 39 | "R_WASM32_NONE", /* name */ |
| 40 | FALSE, /* partial_inplace */ |
| 41 | 0, /* src_mask */ |
| 42 | 0, /* dst_mask */ |
| 43 | FALSE), /* pcrel_offset */ |
| 44 | |
| 45 | /* 32 bit absolute */ |
| 46 | HOWTO (R_WASM32_32, /* type */ |
| 47 | 0, /* rightshift */ |
| 48 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 49 | 32, /* bitsize */ |
| 50 | FALSE, /* pc_relative */ |
| 51 | 0, /* bitpos */ |
| 52 | complain_overflow_bitfield,/* complain_on_overflow */ |
| 53 | bfd_elf_generic_reloc, /* special_function */ |
| 54 | "R_WASM32_32", /* name */ |
| 55 | FALSE, /* partial_inplace */ |
| 56 | 0xffffffff, /* src_mask */ |
| 57 | 0xffffffff, /* dst_mask */ |
| 58 | FALSE), /* pcrel_offset */ |
| 59 | }; |
| 60 | |
| 61 | /* Look up the relocation CODE. */ |
| 62 | |
| 63 | static reloc_howto_type * |
| 64 | elf32_wasm32_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 65 | bfd_reloc_code_real_type code) |
| 66 | { |
| 67 | switch (code) |
| 68 | { |
| 69 | case BFD_RELOC_NONE: |
| 70 | return &elf32_wasm32_howto_table[R_WASM32_NONE]; |
| 71 | case BFD_RELOC_32: |
| 72 | return &elf32_wasm32_howto_table[R_WASM32_32]; |
| 73 | default: |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | /* Look up the relocation R_NAME. */ |
| 81 | |
| 82 | static reloc_howto_type * |
| 83 | elf32_wasm32_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 84 | const char *r_name) |
| 85 | { |
| 86 | unsigned int i; |
| 87 | |
| 88 | for (i = 0; i < ARRAY_SIZE (elf32_wasm32_howto_table); i++) |
| 89 | if (elf32_wasm32_howto_table[i].name != NULL |
| 90 | && strcasecmp (elf32_wasm32_howto_table[i].name, r_name) == 0) |
| 91 | return &elf32_wasm32_howto_table[i]; |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /* Look up the relocation R_TYPE. */ |
| 97 | |
| 98 | static reloc_howto_type * |
| 99 | elf32_wasm32_rtype_to_howto (bfd *abfd, unsigned r_type) |
| 100 | { |
| 101 | unsigned int i = r_type; |
| 102 | |
| 103 | if (i >= ARRAY_SIZE (elf32_wasm32_howto_table)) |
| 104 | { |
| 105 | /* xgettext:c-format */ |
| 106 | _bfd_error_handler (_("%B: invalid relocation type %d"), |
| 107 | abfd, (int) r_type); |
| 108 | i = R_WASM32_NONE; |
| 109 | } |
| 110 | |
| 111 | if (elf32_wasm32_howto_table[i].type != r_type) |
| 112 | return NULL; |
| 113 | |
| 114 | return &elf32_wasm32_howto_table[i]; |
| 115 | } |
| 116 | |
| 117 | /* Translate the ELF-internal relocation RELA into CACHE_PTR. */ |
| 118 | |
| 119 | static void |
| 120 | elf32_wasm32_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED, |
| 121 | arelent *cache_ptr, |
| 122 | Elf_Internal_Rela *dst) |
| 123 | { |
| 124 | unsigned int r_type = ELF32_R_TYPE (dst->r_info); |
| 125 | cache_ptr->howto = elf32_wasm32_rtype_to_howto (abfd, r_type); |
| 126 | } |
| 127 | |
| 128 | #define ELF_ARCH bfd_arch_wasm32 |
| 129 | #define ELF_TARGET_ID EM_WEBASSEMBLY |
| 130 | #define ELF_MACHINE_CODE EM_WEBASSEMBLY |
| 131 | /* FIXME we don't have paged executables, see: |
| 132 | https://github.com/pipcet/binutils-gdb/issues/4 */ |
| 133 | #define ELF_MAXPAGESIZE 4096 |
| 134 | |
| 135 | #define TARGET_LITTLE_SYM wasm32_elf32_vec |
| 136 | #define TARGET_LITTLE_NAME "elf32-wasm32" |
| 137 | |
| 138 | #define elf_backend_can_gc_sections 1 |
| 139 | #define elf_backend_rela_normal 1 |
| 140 | /* For testing. */ |
| 141 | #define elf_backend_want_dynrelro 1 |
| 142 | |
| 143 | #define elf_info_to_howto elf32_wasm32_info_to_howto_rela |
| 144 | #define elf_info_to_howto_rel NULL |
| 145 | |
| 146 | #define bfd_elf32_bfd_reloc_type_lookup elf32_wasm32_reloc_type_lookup |
| 147 | #define bfd_elf32_bfd_reloc_name_lookup elf32_wasm32_reloc_name_lookup |
| 148 | |
| 149 | #define ELF_DYNAMIC_INTERPRETER "/sbin/elf-dynamic-interpreter.so" |
| 150 | |
| 151 | #define elf_backend_want_got_plt 1 |
| 152 | #define elf_backend_plt_readonly 1 |
| 153 | #define elf_backend_got_header_size 0 |
| 154 | |
| 155 | #include "elf32-target.h" |