1 1.1 christos /* AVR-specific support for 32-bit ELF 2 1.11 christos Copyright (C) 1999-2024 Free Software Foundation, Inc. 3 1.1 christos Contributed by Denis Chertykov <denisc (at) overta.ru> 4 1.1 christos 5 1.1 christos This file is part of BFD, the Binary File Descriptor library. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program; if not, write to the Free Software 19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, 20 1.1 christos Boston, MA 02110-1301, USA. */ 21 1.1 christos 22 1.1 christos #include "sysdep.h" 23 1.1 christos #include "bfd.h" 24 1.1 christos #include "libbfd.h" 25 1.1 christos #include "elf-bfd.h" 26 1.1 christos #include "elf/avr.h" 27 1.1 christos #include "elf32-avr.h" 28 1.1 christos 29 1.1 christos /* Enable debugging printout at stdout with this variable. */ 30 1.10 christos static bool debug_relax = false; 31 1.1 christos 32 1.1 christos /* Enable debugging printout at stdout with this variable. */ 33 1.10 christos static bool debug_stubs = false; 34 1.1 christos 35 1.3 christos static bfd_reloc_status_type 36 1.3 christos bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *, 37 1.3 christos asection *, bfd *, char **); 38 1.3 christos 39 1.1 christos /* Hash table initialization and handling. Code is taken from the hppa port 40 1.1 christos and adapted to the needs of AVR. */ 41 1.1 christos 42 1.1 christos /* We use two hash tables to hold information for linking avr objects. 43 1.1 christos 44 1.1 christos The first is the elf32_avr_link_hash_table which is derived from the 45 1.1 christos stanard ELF linker hash table. We use this as a place to attach the other 46 1.1 christos hash table and some static information. 47 1.1 christos 48 1.1 christos The second is the stub hash table which is derived from the base BFD 49 1.1 christos hash table. The stub hash table holds the information on the linker 50 1.1 christos stubs. */ 51 1.1 christos 52 1.1 christos struct elf32_avr_stub_hash_entry 53 1.1 christos { 54 1.1 christos /* Base hash table entry structure. */ 55 1.1 christos struct bfd_hash_entry bh_root; 56 1.1 christos 57 1.1 christos /* Offset within stub_sec of the beginning of this stub. */ 58 1.1 christos bfd_vma stub_offset; 59 1.1 christos 60 1.1 christos /* Given the symbol's value and its section we can determine its final 61 1.1 christos value when building the stubs (so the stub knows where to jump). */ 62 1.1 christos bfd_vma target_value; 63 1.1 christos 64 1.1 christos /* This way we could mark stubs to be no longer necessary. */ 65 1.10 christos bool is_actually_needed; 66 1.1 christos }; 67 1.1 christos 68 1.1 christos struct elf32_avr_link_hash_table 69 1.1 christos { 70 1.1 christos /* The main hash table. */ 71 1.1 christos struct elf_link_hash_table etab; 72 1.1 christos 73 1.1 christos /* The stub hash table. */ 74 1.1 christos struct bfd_hash_table bstab; 75 1.1 christos 76 1.10 christos bool no_stubs; 77 1.1 christos 78 1.1 christos /* Linker stub bfd. */ 79 1.1 christos bfd *stub_bfd; 80 1.1 christos 81 1.1 christos /* The stub section. */ 82 1.1 christos asection *stub_sec; 83 1.1 christos 84 1.1 christos /* Usually 0, unless we are generating code for a bootloader. Will 85 1.1 christos be initialized by elf32_avr_size_stubs to the vma offset of the 86 1.1 christos output section associated with the stub section. */ 87 1.1 christos bfd_vma vector_base; 88 1.1 christos 89 1.1 christos /* Assorted information used by elf32_avr_size_stubs. */ 90 1.8 christos unsigned int bfd_count; 91 1.8 christos unsigned int top_index; 92 1.8 christos asection ** input_list; 93 1.1 christos Elf_Internal_Sym ** all_local_syms; 94 1.1 christos 95 1.1 christos /* Tables for mapping vma beyond the 128k boundary to the address of the 96 1.1 christos corresponding stub. (AMT) 97 1.1 christos "amt_max_entry_cnt" reflects the number of entries that memory is allocated 98 1.1 christos for in the "amt_stub_offsets" and "amt_destination_addr" arrays. 99 1.1 christos "amt_entry_cnt" informs how many of these entries actually contain 100 1.1 christos useful data. */ 101 1.1 christos unsigned int amt_entry_cnt; 102 1.1 christos unsigned int amt_max_entry_cnt; 103 1.1 christos bfd_vma * amt_stub_offsets; 104 1.1 christos bfd_vma * amt_destination_addr; 105 1.1 christos }; 106 1.1 christos 107 1.1 christos /* Various hash macros and functions. */ 108 1.1 christos #define avr_link_hash_table(p) \ 109 1.9 christos ((is_elf_hash_table ((p)->hash) \ 110 1.9 christos && elf_hash_table_id (elf_hash_table (p)) == AVR_ELF_DATA) \ 111 1.9 christos ? (struct elf32_avr_link_hash_table *) (p)->hash : NULL) 112 1.1 christos 113 1.1 christos #define avr_stub_hash_entry(ent) \ 114 1.1 christos ((struct elf32_avr_stub_hash_entry *)(ent)) 115 1.1 christos 116 1.1 christos #define avr_stub_hash_lookup(table, string, create, copy) \ 117 1.1 christos ((struct elf32_avr_stub_hash_entry *) \ 118 1.1 christos bfd_hash_lookup ((table), (string), (create), (copy))) 119 1.1 christos 120 1.1 christos static reloc_howto_type elf_avr_howto_table[] = 121 1.1 christos { 122 1.1 christos HOWTO (R_AVR_NONE, /* type */ 123 1.1 christos 0, /* rightshift */ 124 1.10 christos 0, /* size */ 125 1.5 christos 0, /* bitsize */ 126 1.10 christos false, /* pc_relative */ 127 1.1 christos 0, /* bitpos */ 128 1.5 christos complain_overflow_dont, /* complain_on_overflow */ 129 1.1 christos bfd_elf_generic_reloc, /* special_function */ 130 1.1 christos "R_AVR_NONE", /* name */ 131 1.10 christos false, /* partial_inplace */ 132 1.1 christos 0, /* src_mask */ 133 1.1 christos 0, /* dst_mask */ 134 1.10 christos false), /* pcrel_offset */ 135 1.1 christos 136 1.1 christos HOWTO (R_AVR_32, /* type */ 137 1.1 christos 0, /* rightshift */ 138 1.10 christos 4, /* size */ 139 1.1 christos 32, /* bitsize */ 140 1.10 christos false, /* pc_relative */ 141 1.1 christos 0, /* bitpos */ 142 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 143 1.1 christos bfd_elf_generic_reloc, /* special_function */ 144 1.1 christos "R_AVR_32", /* name */ 145 1.10 christos false, /* partial_inplace */ 146 1.1 christos 0xffffffff, /* src_mask */ 147 1.1 christos 0xffffffff, /* dst_mask */ 148 1.10 christos false), /* pcrel_offset */ 149 1.1 christos 150 1.1 christos /* A 7 bit PC relative relocation. */ 151 1.1 christos HOWTO (R_AVR_7_PCREL, /* type */ 152 1.1 christos 1, /* rightshift */ 153 1.10 christos 2, /* size */ 154 1.1 christos 7, /* bitsize */ 155 1.10 christos true, /* pc_relative */ 156 1.1 christos 3, /* bitpos */ 157 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 158 1.1 christos bfd_elf_generic_reloc, /* special_function */ 159 1.1 christos "R_AVR_7_PCREL", /* name */ 160 1.10 christos false, /* partial_inplace */ 161 1.1 christos 0xffff, /* src_mask */ 162 1.1 christos 0xffff, /* dst_mask */ 163 1.10 christos true), /* pcrel_offset */ 164 1.1 christos 165 1.1 christos /* A 13 bit PC relative relocation. */ 166 1.1 christos HOWTO (R_AVR_13_PCREL, /* type */ 167 1.1 christos 1, /* rightshift */ 168 1.10 christos 2, /* size */ 169 1.1 christos 13, /* bitsize */ 170 1.10 christos true, /* pc_relative */ 171 1.1 christos 0, /* bitpos */ 172 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 173 1.1 christos bfd_elf_generic_reloc, /* special_function */ 174 1.1 christos "R_AVR_13_PCREL", /* name */ 175 1.10 christos false, /* partial_inplace */ 176 1.1 christos 0xfff, /* src_mask */ 177 1.1 christos 0xfff, /* dst_mask */ 178 1.10 christos true), /* pcrel_offset */ 179 1.1 christos 180 1.1 christos /* A 16 bit absolute relocation. */ 181 1.1 christos HOWTO (R_AVR_16, /* type */ 182 1.1 christos 0, /* rightshift */ 183 1.10 christos 2, /* size */ 184 1.1 christos 16, /* bitsize */ 185 1.10 christos false, /* pc_relative */ 186 1.1 christos 0, /* bitpos */ 187 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 188 1.1 christos bfd_elf_generic_reloc, /* special_function */ 189 1.1 christos "R_AVR_16", /* name */ 190 1.10 christos false, /* partial_inplace */ 191 1.1 christos 0xffff, /* src_mask */ 192 1.1 christos 0xffff, /* dst_mask */ 193 1.10 christos false), /* pcrel_offset */ 194 1.1 christos 195 1.1 christos /* A 16 bit absolute relocation for command address 196 1.1 christos Will be changed when linker stubs are needed. */ 197 1.1 christos HOWTO (R_AVR_16_PM, /* type */ 198 1.1 christos 1, /* rightshift */ 199 1.10 christos 2, /* size */ 200 1.1 christos 16, /* bitsize */ 201 1.10 christos false, /* pc_relative */ 202 1.1 christos 0, /* bitpos */ 203 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 204 1.1 christos bfd_elf_generic_reloc, /* special_function */ 205 1.1 christos "R_AVR_16_PM", /* name */ 206 1.10 christos false, /* partial_inplace */ 207 1.1 christos 0xffff, /* src_mask */ 208 1.1 christos 0xffff, /* dst_mask */ 209 1.10 christos false), /* pcrel_offset */ 210 1.1 christos /* A low 8 bit absolute relocation of 16 bit address. 211 1.1 christos For LDI command. */ 212 1.1 christos HOWTO (R_AVR_LO8_LDI, /* type */ 213 1.1 christos 0, /* rightshift */ 214 1.10 christos 2, /* size */ 215 1.1 christos 8, /* bitsize */ 216 1.10 christos false, /* pc_relative */ 217 1.1 christos 0, /* bitpos */ 218 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 219 1.1 christos bfd_elf_generic_reloc, /* special_function */ 220 1.1 christos "R_AVR_LO8_LDI", /* name */ 221 1.10 christos false, /* partial_inplace */ 222 1.1 christos 0xffff, /* src_mask */ 223 1.1 christos 0xffff, /* dst_mask */ 224 1.10 christos false), /* pcrel_offset */ 225 1.1 christos /* A high 8 bit absolute relocation of 16 bit address. 226 1.1 christos For LDI command. */ 227 1.1 christos HOWTO (R_AVR_HI8_LDI, /* type */ 228 1.1 christos 8, /* rightshift */ 229 1.10 christos 2, /* size */ 230 1.1 christos 8, /* bitsize */ 231 1.10 christos false, /* pc_relative */ 232 1.1 christos 0, /* bitpos */ 233 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 234 1.1 christos bfd_elf_generic_reloc, /* special_function */ 235 1.1 christos "R_AVR_HI8_LDI", /* name */ 236 1.10 christos false, /* partial_inplace */ 237 1.1 christos 0xffff, /* src_mask */ 238 1.1 christos 0xffff, /* dst_mask */ 239 1.10 christos false), /* pcrel_offset */ 240 1.1 christos /* A high 6 bit absolute relocation of 22 bit address. 241 1.1 christos For LDI command. As well second most significant 8 bit value of 242 1.1 christos a 32 bit link-time constant. */ 243 1.1 christos HOWTO (R_AVR_HH8_LDI, /* type */ 244 1.1 christos 16, /* rightshift */ 245 1.10 christos 2, /* size */ 246 1.1 christos 8, /* bitsize */ 247 1.10 christos false, /* pc_relative */ 248 1.1 christos 0, /* bitpos */ 249 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 250 1.1 christos bfd_elf_generic_reloc, /* special_function */ 251 1.1 christos "R_AVR_HH8_LDI", /* name */ 252 1.10 christos false, /* partial_inplace */ 253 1.1 christos 0xffff, /* src_mask */ 254 1.1 christos 0xffff, /* dst_mask */ 255 1.10 christos false), /* pcrel_offset */ 256 1.1 christos /* A negative low 8 bit absolute relocation of 16 bit address. 257 1.1 christos For LDI command. */ 258 1.1 christos HOWTO (R_AVR_LO8_LDI_NEG, /* type */ 259 1.1 christos 0, /* rightshift */ 260 1.10 christos 2, /* size */ 261 1.1 christos 8, /* bitsize */ 262 1.10 christos false, /* pc_relative */ 263 1.1 christos 0, /* bitpos */ 264 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 265 1.1 christos bfd_elf_generic_reloc, /* special_function */ 266 1.1 christos "R_AVR_LO8_LDI_NEG", /* name */ 267 1.10 christos false, /* partial_inplace */ 268 1.1 christos 0xffff, /* src_mask */ 269 1.1 christos 0xffff, /* dst_mask */ 270 1.10 christos false), /* pcrel_offset */ 271 1.1 christos /* A negative high 8 bit absolute relocation of 16 bit address. 272 1.1 christos For LDI command. */ 273 1.1 christos HOWTO (R_AVR_HI8_LDI_NEG, /* type */ 274 1.1 christos 8, /* rightshift */ 275 1.10 christos 2, /* size */ 276 1.1 christos 8, /* bitsize */ 277 1.10 christos false, /* pc_relative */ 278 1.1 christos 0, /* bitpos */ 279 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 280 1.1 christos bfd_elf_generic_reloc, /* special_function */ 281 1.1 christos "R_AVR_HI8_LDI_NEG", /* name */ 282 1.10 christos false, /* partial_inplace */ 283 1.1 christos 0xffff, /* src_mask */ 284 1.1 christos 0xffff, /* dst_mask */ 285 1.10 christos false), /* pcrel_offset */ 286 1.1 christos /* A negative high 6 bit absolute relocation of 22 bit address. 287 1.1 christos For LDI command. */ 288 1.1 christos HOWTO (R_AVR_HH8_LDI_NEG, /* type */ 289 1.1 christos 16, /* rightshift */ 290 1.10 christos 2, /* size */ 291 1.1 christos 8, /* bitsize */ 292 1.10 christos false, /* pc_relative */ 293 1.1 christos 0, /* bitpos */ 294 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 295 1.1 christos bfd_elf_generic_reloc, /* special_function */ 296 1.1 christos "R_AVR_HH8_LDI_NEG", /* name */ 297 1.10 christos false, /* partial_inplace */ 298 1.1 christos 0xffff, /* src_mask */ 299 1.1 christos 0xffff, /* dst_mask */ 300 1.10 christos false), /* pcrel_offset */ 301 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 302 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 303 1.1 christos HOWTO (R_AVR_LO8_LDI_PM, /* type */ 304 1.1 christos 1, /* rightshift */ 305 1.10 christos 2, /* size */ 306 1.1 christos 8, /* bitsize */ 307 1.10 christos false, /* pc_relative */ 308 1.1 christos 0, /* bitpos */ 309 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 310 1.1 christos bfd_elf_generic_reloc, /* special_function */ 311 1.1 christos "R_AVR_LO8_LDI_PM", /* name */ 312 1.10 christos false, /* partial_inplace */ 313 1.1 christos 0xffff, /* src_mask */ 314 1.1 christos 0xffff, /* dst_mask */ 315 1.10 christos false), /* pcrel_offset */ 316 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 317 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 318 1.1 christos HOWTO (R_AVR_HI8_LDI_PM, /* type */ 319 1.1 christos 9, /* rightshift */ 320 1.10 christos 2, /* size */ 321 1.1 christos 8, /* bitsize */ 322 1.10 christos false, /* pc_relative */ 323 1.1 christos 0, /* bitpos */ 324 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 325 1.1 christos bfd_elf_generic_reloc, /* special_function */ 326 1.1 christos "R_AVR_HI8_LDI_PM", /* name */ 327 1.10 christos false, /* partial_inplace */ 328 1.1 christos 0xffff, /* src_mask */ 329 1.1 christos 0xffff, /* dst_mask */ 330 1.10 christos false), /* pcrel_offset */ 331 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 332 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 333 1.1 christos HOWTO (R_AVR_HH8_LDI_PM, /* type */ 334 1.1 christos 17, /* rightshift */ 335 1.10 christos 2, /* size */ 336 1.1 christos 8, /* bitsize */ 337 1.10 christos false, /* pc_relative */ 338 1.1 christos 0, /* bitpos */ 339 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 340 1.1 christos bfd_elf_generic_reloc, /* special_function */ 341 1.1 christos "R_AVR_HH8_LDI_PM", /* name */ 342 1.10 christos false, /* partial_inplace */ 343 1.1 christos 0xffff, /* src_mask */ 344 1.1 christos 0xffff, /* dst_mask */ 345 1.10 christos false), /* pcrel_offset */ 346 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 347 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 348 1.1 christos HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */ 349 1.1 christos 1, /* rightshift */ 350 1.10 christos 2, /* size */ 351 1.1 christos 8, /* bitsize */ 352 1.10 christos false, /* pc_relative */ 353 1.1 christos 0, /* bitpos */ 354 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 355 1.1 christos bfd_elf_generic_reloc, /* special_function */ 356 1.1 christos "R_AVR_LO8_LDI_PM_NEG", /* name */ 357 1.10 christos false, /* partial_inplace */ 358 1.1 christos 0xffff, /* src_mask */ 359 1.1 christos 0xffff, /* dst_mask */ 360 1.10 christos false), /* pcrel_offset */ 361 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 362 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 363 1.1 christos HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */ 364 1.1 christos 9, /* rightshift */ 365 1.10 christos 2, /* size */ 366 1.1 christos 8, /* bitsize */ 367 1.10 christos false, /* pc_relative */ 368 1.1 christos 0, /* bitpos */ 369 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 370 1.1 christos bfd_elf_generic_reloc, /* special_function */ 371 1.1 christos "R_AVR_HI8_LDI_PM_NEG", /* name */ 372 1.10 christos false, /* partial_inplace */ 373 1.1 christos 0xffff, /* src_mask */ 374 1.1 christos 0xffff, /* dst_mask */ 375 1.10 christos false), /* pcrel_offset */ 376 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 377 1.1 christos For LDI command. Will not be changed when linker stubs are needed. */ 378 1.1 christos HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */ 379 1.1 christos 17, /* rightshift */ 380 1.10 christos 2, /* size */ 381 1.1 christos 8, /* bitsize */ 382 1.10 christos false, /* pc_relative */ 383 1.1 christos 0, /* bitpos */ 384 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 385 1.1 christos bfd_elf_generic_reloc, /* special_function */ 386 1.1 christos "R_AVR_HH8_LDI_PM_NEG", /* name */ 387 1.10 christos false, /* partial_inplace */ 388 1.1 christos 0xffff, /* src_mask */ 389 1.1 christos 0xffff, /* dst_mask */ 390 1.10 christos false), /* pcrel_offset */ 391 1.1 christos /* Relocation for CALL command in ATmega. */ 392 1.1 christos HOWTO (R_AVR_CALL, /* type */ 393 1.1 christos 1, /* rightshift */ 394 1.10 christos 4, /* size */ 395 1.1 christos 23, /* bitsize */ 396 1.10 christos false, /* pc_relative */ 397 1.1 christos 0, /* bitpos */ 398 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 399 1.1 christos bfd_elf_generic_reloc, /* special_function */ 400 1.1 christos "R_AVR_CALL", /* name */ 401 1.10 christos false, /* partial_inplace */ 402 1.1 christos 0xffffffff, /* src_mask */ 403 1.1 christos 0xffffffff, /* dst_mask */ 404 1.10 christos false), /* pcrel_offset */ 405 1.1 christos /* A 16 bit absolute relocation of 16 bit address. 406 1.1 christos For LDI command. */ 407 1.1 christos HOWTO (R_AVR_LDI, /* type */ 408 1.1 christos 0, /* rightshift */ 409 1.10 christos 2, /* size */ 410 1.1 christos 16, /* bitsize */ 411 1.10 christos false, /* pc_relative */ 412 1.1 christos 0, /* bitpos */ 413 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 414 1.1 christos bfd_elf_generic_reloc, /* special_function */ 415 1.1 christos "R_AVR_LDI", /* name */ 416 1.10 christos false, /* partial_inplace */ 417 1.1 christos 0xffff, /* src_mask */ 418 1.1 christos 0xffff, /* dst_mask */ 419 1.10 christos false), /* pcrel_offset */ 420 1.1 christos /* A 6 bit absolute relocation of 6 bit offset. 421 1.1 christos For ldd/sdd command. */ 422 1.1 christos HOWTO (R_AVR_6, /* type */ 423 1.1 christos 0, /* rightshift */ 424 1.10 christos 1, /* size */ 425 1.1 christos 6, /* bitsize */ 426 1.10 christos false, /* pc_relative */ 427 1.1 christos 0, /* bitpos */ 428 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 429 1.1 christos bfd_elf_generic_reloc, /* special_function */ 430 1.1 christos "R_AVR_6", /* name */ 431 1.10 christos false, /* partial_inplace */ 432 1.1 christos 0xffff, /* src_mask */ 433 1.1 christos 0xffff, /* dst_mask */ 434 1.10 christos false), /* pcrel_offset */ 435 1.1 christos /* A 6 bit absolute relocation of 6 bit offset. 436 1.1 christos For sbiw/adiw command. */ 437 1.1 christos HOWTO (R_AVR_6_ADIW, /* type */ 438 1.1 christos 0, /* rightshift */ 439 1.10 christos 1, /* size */ 440 1.1 christos 6, /* bitsize */ 441 1.10 christos false, /* pc_relative */ 442 1.1 christos 0, /* bitpos */ 443 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 444 1.1 christos bfd_elf_generic_reloc, /* special_function */ 445 1.1 christos "R_AVR_6_ADIW", /* name */ 446 1.10 christos false, /* partial_inplace */ 447 1.1 christos 0xffff, /* src_mask */ 448 1.1 christos 0xffff, /* dst_mask */ 449 1.10 christos false), /* pcrel_offset */ 450 1.1 christos /* Most significant 8 bit value of a 32 bit link-time constant. */ 451 1.1 christos HOWTO (R_AVR_MS8_LDI, /* type */ 452 1.1 christos 24, /* rightshift */ 453 1.10 christos 2, /* size */ 454 1.1 christos 8, /* bitsize */ 455 1.10 christos false, /* pc_relative */ 456 1.1 christos 0, /* bitpos */ 457 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 458 1.1 christos bfd_elf_generic_reloc, /* special_function */ 459 1.1 christos "R_AVR_MS8_LDI", /* name */ 460 1.10 christos false, /* partial_inplace */ 461 1.1 christos 0xffff, /* src_mask */ 462 1.1 christos 0xffff, /* dst_mask */ 463 1.10 christos false), /* pcrel_offset */ 464 1.1 christos /* Negative most significant 8 bit value of a 32 bit link-time constant. */ 465 1.1 christos HOWTO (R_AVR_MS8_LDI_NEG, /* type */ 466 1.1 christos 24, /* rightshift */ 467 1.10 christos 2, /* size */ 468 1.1 christos 8, /* bitsize */ 469 1.10 christos false, /* pc_relative */ 470 1.1 christos 0, /* bitpos */ 471 1.1 christos complain_overflow_dont, /* complain_on_overflow */ 472 1.1 christos bfd_elf_generic_reloc, /* special_function */ 473 1.1 christos "R_AVR_MS8_LDI_NEG", /* name */ 474 1.10 christos false, /* partial_inplace */ 475 1.1 christos 0xffff, /* src_mask */ 476 1.1 christos 0xffff, /* dst_mask */ 477 1.10 christos false), /* pcrel_offset */ 478 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 479 1.1 christos For LDI command. Will be changed when linker stubs are needed. */ 480 1.8 christos HOWTO (R_AVR_LO8_LDI_GS, /* type */ 481 1.8 christos 1, /* rightshift */ 482 1.10 christos 2, /* size */ 483 1.8 christos 8, /* bitsize */ 484 1.10 christos false, /* pc_relative */ 485 1.8 christos 0, /* bitpos */ 486 1.8 christos complain_overflow_dont, /* complain_on_overflow */ 487 1.8 christos bfd_elf_generic_reloc, /* special_function */ 488 1.8 christos "R_AVR_LO8_LDI_GS", /* name */ 489 1.10 christos false, /* partial_inplace */ 490 1.8 christos 0xffff, /* src_mask */ 491 1.8 christos 0xffff, /* dst_mask */ 492 1.10 christos false), /* pcrel_offset */ 493 1.1 christos /* A low 8 bit absolute relocation of 24 bit program memory address. 494 1.1 christos For LDI command. Will be changed when linker stubs are needed. */ 495 1.8 christos HOWTO (R_AVR_HI8_LDI_GS, /* type */ 496 1.8 christos 9, /* rightshift */ 497 1.10 christos 2, /* size */ 498 1.8 christos 8, /* bitsize */ 499 1.10 christos false, /* pc_relative */ 500 1.8 christos 0, /* bitpos */ 501 1.8 christos complain_overflow_dont, /* complain_on_overflow */ 502 1.8 christos bfd_elf_generic_reloc, /* special_function */ 503 1.8 christos "R_AVR_HI8_LDI_GS", /* name */ 504 1.10 christos false, /* partial_inplace */ 505 1.8 christos 0xffff, /* src_mask */ 506 1.8 christos 0xffff, /* dst_mask */ 507 1.10 christos false), /* pcrel_offset */ 508 1.1 christos /* 8 bit offset. */ 509 1.1 christos HOWTO (R_AVR_8, /* type */ 510 1.1 christos 0, /* rightshift */ 511 1.10 christos 1, /* size */ 512 1.1 christos 8, /* bitsize */ 513 1.10 christos false, /* pc_relative */ 514 1.1 christos 0, /* bitpos */ 515 1.1 christos complain_overflow_bitfield,/* complain_on_overflow */ 516 1.1 christos bfd_elf_generic_reloc, /* special_function */ 517 1.1 christos "R_AVR_8", /* name */ 518 1.10 christos false, /* partial_inplace */ 519 1.1 christos 0x000000ff, /* src_mask */ 520 1.1 christos 0x000000ff, /* dst_mask */ 521 1.10 christos false), /* pcrel_offset */ 522 1.1 christos /* lo8-part to use in .byte lo8(sym). */ 523 1.1 christos HOWTO (R_AVR_8_LO8, /* type */ 524 1.1 christos 0, /* rightshift */ 525 1.10 christos 1, /* size */ 526 1.1 christos 8, /* bitsize */ 527 1.10 christos false, /* pc_relative */ 528 1.1 christos 0, /* bitpos */ 529 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 530 1.1 christos bfd_elf_generic_reloc, /* special_function */ 531 1.1 christos "R_AVR_8_LO8", /* name */ 532 1.10 christos false, /* partial_inplace */ 533 1.1 christos 0xffffff, /* src_mask */ 534 1.1 christos 0xffffff, /* dst_mask */ 535 1.10 christos false), /* pcrel_offset */ 536 1.1 christos /* hi8-part to use in .byte hi8(sym). */ 537 1.1 christos HOWTO (R_AVR_8_HI8, /* type */ 538 1.1 christos 8, /* rightshift */ 539 1.10 christos 1, /* size */ 540 1.1 christos 8, /* bitsize */ 541 1.10 christos false, /* pc_relative */ 542 1.1 christos 0, /* bitpos */ 543 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 544 1.1 christos bfd_elf_generic_reloc, /* special_function */ 545 1.1 christos "R_AVR_8_HI8", /* name */ 546 1.10 christos false, /* partial_inplace */ 547 1.1 christos 0xffffff, /* src_mask */ 548 1.1 christos 0xffffff, /* dst_mask */ 549 1.10 christos false), /* pcrel_offset */ 550 1.1 christos /* hlo8-part to use in .byte hlo8(sym). */ 551 1.1 christos HOWTO (R_AVR_8_HLO8, /* type */ 552 1.1 christos 16, /* rightshift */ 553 1.10 christos 1, /* size */ 554 1.1 christos 8, /* bitsize */ 555 1.10 christos false, /* pc_relative */ 556 1.1 christos 0, /* bitpos */ 557 1.1 christos complain_overflow_dont,/* complain_on_overflow */ 558 1.1 christos bfd_elf_generic_reloc, /* special_function */ 559 1.1 christos "R_AVR_8_HLO8", /* name */ 560 1.10 christos false, /* partial_inplace */ 561 1.1 christos 0xffffff, /* src_mask */ 562 1.1 christos 0xffffff, /* dst_mask */ 563 1.10 christos false), /* pcrel_offset */ 564 1.3 christos HOWTO (R_AVR_DIFF8, /* type */ 565 1.8 christos 0, /* rightshift */ 566 1.10 christos 1, /* size */ 567 1.8 christos 8, /* bitsize */ 568 1.10 christos false, /* pc_relative */ 569 1.8 christos 0, /* bitpos */ 570 1.3 christos complain_overflow_bitfield, /* complain_on_overflow */ 571 1.3 christos bfd_elf_avr_diff_reloc, /* special_function */ 572 1.8 christos "R_AVR_DIFF8", /* name */ 573 1.10 christos false, /* partial_inplace */ 574 1.8 christos 0, /* src_mask */ 575 1.8 christos 0xff, /* dst_mask */ 576 1.10 christos false), /* pcrel_offset */ 577 1.8 christos HOWTO (R_AVR_DIFF16, /* type */ 578 1.8 christos 0, /* rightshift */ 579 1.10 christos 2, /* size */ 580 1.3 christos 16, /* bitsize */ 581 1.10 christos false, /* pc_relative */ 582 1.8 christos 0, /* bitpos */ 583 1.3 christos complain_overflow_bitfield, /* complain_on_overflow */ 584 1.3 christos bfd_elf_avr_diff_reloc,/* special_function */ 585 1.8 christos "R_AVR_DIFF16", /* name */ 586 1.10 christos false, /* partial_inplace */ 587 1.8 christos 0, /* src_mask */ 588 1.8 christos 0xffff, /* dst_mask */ 589 1.10 christos false), /* pcrel_offset */ 590 1.8 christos HOWTO (R_AVR_DIFF32, /* type */ 591 1.8 christos 0, /* rightshift */ 592 1.10 christos 4, /* size */ 593 1.8 christos 32, /* bitsize */ 594 1.10 christos false, /* pc_relative */ 595 1.8 christos 0, /* bitpos */ 596 1.3 christos complain_overflow_bitfield, /* complain_on_overflow */ 597 1.3 christos bfd_elf_avr_diff_reloc,/* special_function */ 598 1.8 christos "R_AVR_DIFF32", /* name */ 599 1.10 christos false, /* partial_inplace */ 600 1.8 christos 0, /* src_mask */ 601 1.8 christos 0xffffffff, /* dst_mask */ 602 1.10 christos false), /* pcrel_offset */ 603 1.3 christos /* 7 bit immediate for LDS/STS in Tiny core. */ 604 1.3 christos HOWTO (R_AVR_LDS_STS_16, /* type */ 605 1.8 christos 0, /* rightshift */ 606 1.10 christos 2, /* size */ 607 1.8 christos 7, /* bitsize */ 608 1.10 christos false, /* pc_relative */ 609 1.8 christos 0, /* bitpos */ 610 1.3 christos complain_overflow_dont,/* complain_on_overflow */ 611 1.3 christos bfd_elf_generic_reloc, /* special_function */ 612 1.8 christos "R_AVR_LDS_STS_16", /* name */ 613 1.10 christos false, /* partial_inplace */ 614 1.8 christos 0xffff, /* src_mask */ 615 1.8 christos 0xffff, /* dst_mask */ 616 1.10 christos false), /* pcrel_offset */ 617 1.3 christos 618 1.3 christos HOWTO (R_AVR_PORT6, /* type */ 619 1.3 christos 0, /* rightshift */ 620 1.10 christos 1, /* size */ 621 1.3 christos 6, /* bitsize */ 622 1.10 christos false, /* pc_relative */ 623 1.3 christos 0, /* bitpos */ 624 1.3 christos complain_overflow_dont,/* complain_on_overflow */ 625 1.3 christos bfd_elf_generic_reloc, /* special_function */ 626 1.3 christos "R_AVR_PORT6", /* name */ 627 1.10 christos false, /* partial_inplace */ 628 1.3 christos 0xffffff, /* src_mask */ 629 1.3 christos 0xffffff, /* dst_mask */ 630 1.10 christos false), /* pcrel_offset */ 631 1.3 christos HOWTO (R_AVR_PORT5, /* type */ 632 1.3 christos 0, /* rightshift */ 633 1.10 christos 1, /* size */ 634 1.3 christos 5, /* bitsize */ 635 1.10 christos false, /* pc_relative */ 636 1.3 christos 0, /* bitpos */ 637 1.3 christos complain_overflow_dont,/* complain_on_overflow */ 638 1.3 christos bfd_elf_generic_reloc, /* special_function */ 639 1.3 christos "R_AVR_PORT5", /* name */ 640 1.10 christos false, /* partial_inplace */ 641 1.3 christos 0xffffff, /* src_mask */ 642 1.3 christos 0xffffff, /* dst_mask */ 643 1.10 christos false), /* pcrel_offset */ 644 1.6 christos 645 1.6 christos /* A 32 bit PC relative relocation. */ 646 1.6 christos HOWTO (R_AVR_32_PCREL, /* type */ 647 1.8 christos 0, /* rightshift */ 648 1.10 christos 4, /* size */ 649 1.6 christos 32, /* bitsize */ 650 1.10 christos true, /* pc_relative */ 651 1.8 christos 0, /* bitpos */ 652 1.6 christos complain_overflow_bitfield, /* complain_on_overflow */ 653 1.6 christos bfd_elf_generic_reloc, /* special_function */ 654 1.6 christos "R_AVR_32_PCREL", /* name */ 655 1.10 christos false, /* partial_inplace */ 656 1.8 christos 0xffffffff, /* src_mask */ 657 1.8 christos 0xffffffff, /* dst_mask */ 658 1.10 christos true), /* pcrel_offset */ 659 1.1 christos }; 660 1.1 christos 661 1.1 christos /* Map BFD reloc types to AVR ELF reloc types. */ 662 1.1 christos 663 1.1 christos struct avr_reloc_map 664 1.1 christos { 665 1.1 christos bfd_reloc_code_real_type bfd_reloc_val; 666 1.1 christos unsigned int elf_reloc_val; 667 1.1 christos }; 668 1.1 christos 669 1.1 christos static const struct avr_reloc_map avr_reloc_map[] = 670 1.1 christos { 671 1.8 christos { BFD_RELOC_NONE, R_AVR_NONE }, 672 1.8 christos { BFD_RELOC_32, R_AVR_32 }, 673 1.8 christos { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL }, 674 1.8 christos { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL }, 675 1.8 christos { BFD_RELOC_16, R_AVR_16 }, 676 1.8 christos { BFD_RELOC_AVR_16_PM, R_AVR_16_PM }, 677 1.8 christos { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI}, 678 1.8 christos { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI }, 679 1.8 christos { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI }, 680 1.8 christos { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI }, 681 1.8 christos { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG }, 682 1.8 christos { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG }, 683 1.8 christos { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG }, 684 1.8 christos { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG }, 685 1.8 christos { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM }, 686 1.8 christos { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS }, 687 1.8 christos { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM }, 688 1.8 christos { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS }, 689 1.8 christos { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM }, 690 1.1 christos { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG }, 691 1.1 christos { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG }, 692 1.1 christos { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG }, 693 1.8 christos { BFD_RELOC_AVR_CALL, R_AVR_CALL }, 694 1.8 christos { BFD_RELOC_AVR_LDI, R_AVR_LDI }, 695 1.8 christos { BFD_RELOC_AVR_6, R_AVR_6 }, 696 1.8 christos { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }, 697 1.8 christos { BFD_RELOC_8, R_AVR_8 }, 698 1.8 christos { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 }, 699 1.8 christos { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 }, 700 1.8 christos { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 }, 701 1.8 christos { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 }, 702 1.8 christos { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 }, 703 1.8 christos { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 }, 704 1.8 christos { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16}, 705 1.8 christos { BFD_RELOC_AVR_PORT6, R_AVR_PORT6}, 706 1.8 christos { BFD_RELOC_AVR_PORT5, R_AVR_PORT5}, 707 1.8 christos { BFD_RELOC_32_PCREL, R_AVR_32_PCREL} 708 1.8 christos }; 709 1.8 christos 710 1.8 christos static const struct bfd_elf_special_section elf_avr_special_sections[] = 711 1.8 christos { 712 1.8 christos { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 713 1.8 christos { NULL, 0, 0, 0, 0 } 714 1.1 christos }; 715 1.1 christos 716 1.1 christos /* Meant to be filled one day with the wrap around address for the 717 1.1 christos specific device. I.e. should get the value 0x4000 for 16k devices, 718 1.1 christos 0x8000 for 32k devices and so on. 719 1.1 christos 720 1.1 christos We initialize it here with a value of 0x1000000 resulting in 721 1.1 christos that we will never suggest a wrap-around jump during relaxation. 722 1.1 christos The logic of the source code later on assumes that in 723 1.1 christos avr_pc_wrap_around one single bit is set. */ 724 1.1 christos static bfd_vma avr_pc_wrap_around = 0x10000000; 725 1.1 christos 726 1.1 christos /* If this variable holds a value different from zero, the linker relaxation 727 1.1 christos machine will try to optimize call/ret sequences by a single jump 728 1.1 christos instruction. This option could be switched off by a linker switch. */ 729 1.1 christos static int avr_replace_call_ret_sequences = 1; 730 1.1 christos 731 1.5 christos 733 1.5 christos /* Per-section relaxation related information for avr. */ 734 1.5 christos 735 1.5 christos struct avr_relax_info 736 1.5 christos { 737 1.5 christos /* Track the avr property records that apply to this section. */ 738 1.5 christos 739 1.5 christos struct 740 1.5 christos { 741 1.5 christos /* Number of records in the list. */ 742 1.5 christos unsigned count; 743 1.5 christos 744 1.5 christos /* How many records worth of space have we allocated. */ 745 1.5 christos unsigned allocated; 746 1.5 christos 747 1.5 christos /* The records, only COUNT records are initialised. */ 748 1.5 christos struct avr_property_record *items; 749 1.5 christos } records; 750 1.5 christos }; 751 1.5 christos 752 1.5 christos /* Per section data, specialised for avr. */ 753 1.5 christos 754 1.5 christos struct elf_avr_section_data 755 1.5 christos { 756 1.5 christos /* The standard data must appear first. */ 757 1.5 christos struct bfd_elf_section_data elf; 758 1.5 christos 759 1.5 christos /* Relaxation related information. */ 760 1.5 christos struct avr_relax_info relax_info; 761 1.5 christos }; 762 1.5 christos 763 1.5 christos /* Possibly initialise avr specific data for new section SEC from ABFD. */ 764 1.10 christos 765 1.5 christos static bool 766 1.5 christos elf_avr_new_section_hook (bfd *abfd, asection *sec) 767 1.5 christos { 768 1.5 christos if (!sec->used_by_bfd) 769 1.5 christos { 770 1.9 christos struct elf_avr_section_data *sdata; 771 1.5 christos size_t amt = sizeof (*sdata); 772 1.5 christos 773 1.5 christos sdata = bfd_zalloc (abfd, amt); 774 1.10 christos if (sdata == NULL) 775 1.5 christos return false; 776 1.5 christos sec->used_by_bfd = sdata; 777 1.5 christos } 778 1.5 christos 779 1.5 christos return _bfd_elf_new_section_hook (abfd, sec); 780 1.5 christos } 781 1.5 christos 782 1.5 christos /* Return a pointer to the relaxation information for SEC. */ 783 1.5 christos 784 1.5 christos static struct avr_relax_info * 785 1.5 christos get_avr_relax_info (asection *sec) 786 1.5 christos { 787 1.5 christos struct elf_avr_section_data *section_data; 788 1.5 christos 789 1.5 christos /* No info available if no section or if it is an output section. */ 790 1.5 christos if (!sec || sec == sec->output_section) 791 1.5 christos return NULL; 792 1.5 christos 793 1.5 christos section_data = (struct elf_avr_section_data *) elf_section_data (sec); 794 1.5 christos return §ion_data->relax_info; 795 1.5 christos } 796 1.5 christos 797 1.5 christos /* Initialise the per section relaxation information for SEC. */ 798 1.5 christos 799 1.5 christos static void 800 1.5 christos init_avr_relax_info (asection *sec) 801 1.5 christos { 802 1.5 christos struct avr_relax_info *relax_info = get_avr_relax_info (sec); 803 1.5 christos 804 1.5 christos relax_info->records.count = 0; 805 1.5 christos relax_info->records.allocated = 0; 806 1.5 christos relax_info->records.items = NULL; 807 1.5 christos } 808 1.1 christos 809 1.1 christos /* Initialize an entry in the stub hash table. */ 810 1.1 christos 811 1.1 christos static struct bfd_hash_entry * 812 1.8 christos stub_hash_newfunc (struct bfd_hash_entry *entry, 813 1.8 christos struct bfd_hash_table *table, 814 1.1 christos const char *string) 815 1.1 christos { 816 1.1 christos /* Allocate the structure if it has not already been allocated by a 817 1.1 christos subclass. */ 818 1.1 christos if (entry == NULL) 819 1.1 christos { 820 1.8 christos entry = bfd_hash_allocate (table, 821 1.1 christos sizeof (struct elf32_avr_stub_hash_entry)); 822 1.8 christos if (entry == NULL) 823 1.1 christos return entry; 824 1.1 christos } 825 1.1 christos 826 1.1 christos /* Call the allocation method of the superclass. */ 827 1.1 christos entry = bfd_hash_newfunc (entry, table, string); 828 1.1 christos if (entry != NULL) 829 1.1 christos { 830 1.1 christos struct elf32_avr_stub_hash_entry *hsh; 831 1.1 christos 832 1.1 christos /* Initialize the local fields. */ 833 1.1 christos hsh = avr_stub_hash_entry (entry); 834 1.1 christos hsh->stub_offset = 0; 835 1.1 christos hsh->target_value = 0; 836 1.1 christos } 837 1.1 christos 838 1.1 christos return entry; 839 1.1 christos } 840 1.1 christos 841 1.1 christos /* This function is just a straight passthrough to the real 842 1.1 christos function in linker.c. Its prupose is so that its address 843 1.1 christos can be compared inside the avr_link_hash_table macro. */ 844 1.1 christos 845 1.1 christos static struct bfd_hash_entry * 846 1.1 christos elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry, 847 1.1 christos struct bfd_hash_table * table, 848 1.1 christos const char * string) 849 1.1 christos { 850 1.1 christos return _bfd_elf_link_hash_newfunc (entry, table, string); 851 1.1 christos } 852 1.3 christos 853 1.3 christos /* Free the derived linker hash table. */ 854 1.3 christos 855 1.3 christos static void 856 1.3 christos elf32_avr_link_hash_table_free (bfd *obfd) 857 1.3 christos { 858 1.3 christos struct elf32_avr_link_hash_table *htab 859 1.3 christos = (struct elf32_avr_link_hash_table *) obfd->link.hash; 860 1.3 christos 861 1.9 christos /* Free the address mapping table. */ 862 1.9 christos free (htab->amt_stub_offsets); 863 1.3 christos free (htab->amt_destination_addr); 864 1.3 christos 865 1.3 christos bfd_hash_table_free (&htab->bstab); 866 1.3 christos _bfd_elf_link_hash_table_free (obfd); 867 1.3 christos } 868 1.1 christos 869 1.1 christos /* Create the derived linker hash table. The AVR ELF port uses the derived 870 1.1 christos hash table to keep information specific to the AVR ELF linker (without 871 1.1 christos using static variables). */ 872 1.1 christos 873 1.1 christos static struct bfd_link_hash_table * 874 1.1 christos elf32_avr_link_hash_table_create (bfd *abfd) 875 1.1 christos { 876 1.9 christos struct elf32_avr_link_hash_table *htab; 877 1.1 christos size_t amt = sizeof (*htab); 878 1.1 christos 879 1.1 christos htab = bfd_zmalloc (amt); 880 1.1 christos if (htab == NULL) 881 1.1 christos return NULL; 882 1.1 christos 883 1.8 christos if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, 884 1.8 christos elf32_avr_link_hash_newfunc, 885 1.1 christos sizeof (struct elf_link_hash_entry), 886 1.1 christos AVR_ELF_DATA)) 887 1.1 christos { 888 1.1 christos free (htab); 889 1.1 christos return NULL; 890 1.1 christos } 891 1.1 christos 892 1.1 christos /* Init the stub hash table too. */ 893 1.8 christos if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc, 894 1.3 christos sizeof (struct elf32_avr_stub_hash_entry))) 895 1.3 christos { 896 1.3 christos _bfd_elf_link_hash_table_free (abfd); 897 1.3 christos return NULL; 898 1.3 christos } 899 1.1 christos htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free; 900 1.1 christos 901 1.1 christos return &htab->etab.root; 902 1.1 christos } 903 1.1 christos 904 1.1 christos /* Calculates the effective distance of a pc relative jump/call. */ 905 1.1 christos 906 1.1 christos static int 907 1.1 christos avr_relative_distance_considering_wrap_around (unsigned int distance) 908 1.1 christos { 909 1.1 christos unsigned int wrap_around_mask = avr_pc_wrap_around - 1; 910 1.1 christos int dist_with_wrap_around = distance & wrap_around_mask; 911 1.9 christos 912 1.1 christos if (dist_with_wrap_around >= ((int) (avr_pc_wrap_around >> 1))) 913 1.1 christos dist_with_wrap_around -= avr_pc_wrap_around; 914 1.1 christos 915 1.1 christos return dist_with_wrap_around; 916 1.1 christos } 917 1.1 christos 918 1.1 christos 919 1.1 christos static reloc_howto_type * 920 1.1 christos bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 921 1.1 christos bfd_reloc_code_real_type code) 922 1.1 christos { 923 1.1 christos unsigned int i; 924 1.1 christos 925 1.1 christos for (i = 0; 926 1.1 christos i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map); 927 1.1 christos i++) 928 1.1 christos if (avr_reloc_map[i].bfd_reloc_val == code) 929 1.1 christos return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val]; 930 1.1 christos 931 1.1 christos return NULL; 932 1.1 christos } 933 1.1 christos 934 1.1 christos static reloc_howto_type * 935 1.1 christos bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 936 1.1 christos const char *r_name) 937 1.1 christos { 938 1.1 christos unsigned int i; 939 1.1 christos 940 1.1 christos for (i = 0; 941 1.1 christos i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]); 942 1.1 christos i++) 943 1.1 christos if (elf_avr_howto_table[i].name != NULL 944 1.1 christos && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0) 945 1.1 christos return &elf_avr_howto_table[i]; 946 1.1 christos 947 1.1 christos return NULL; 948 1.1 christos } 949 1.1 christos 950 1.1 christos /* Set the howto pointer for an AVR ELF reloc. */ 951 1.10 christos 952 1.8 christos static bool 953 1.1 christos avr_info_to_howto_rela (bfd *abfd, 954 1.1 christos arelent *cache_ptr, 955 1.1 christos Elf_Internal_Rela *dst) 956 1.1 christos { 957 1.1 christos unsigned int r_type; 958 1.1 christos 959 1.3 christos r_type = ELF32_R_TYPE (dst->r_info); 960 1.3 christos if (r_type >= (unsigned int) R_AVR_max) 961 1.7 christos { 962 1.8 christos /* xgettext:c-format */ 963 1.8 christos _bfd_error_handler (_("%pB: unsupported relocation type %#x"), 964 1.8 christos abfd, r_type); 965 1.10 christos bfd_set_error (bfd_error_bad_value); 966 1.3 christos return false; 967 1.1 christos } 968 1.10 christos cache_ptr->howto = &elf_avr_howto_table[r_type]; 969 1.1 christos return true; 970 1.1 christos } 971 1.10 christos 972 1.1 christos static bool 973 1.1 christos avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation) 974 1.1 christos { 975 1.1 christos return (relocation >= 0x020000); 976 1.1 christos } 977 1.1 christos 978 1.1 christos /* Returns the address of the corresponding stub if there is one. 979 1.1 christos Returns otherwise an address above 0x020000. This function 980 1.1 christos could also be used, if there is no knowledge on the section where 981 1.1 christos the destination is found. */ 982 1.1 christos 983 1.1 christos static bfd_vma 984 1.8 christos avr_get_stub_addr (bfd_vma srel, 985 1.1 christos struct elf32_avr_link_hash_table *htab) 986 1.1 christos { 987 1.1 christos unsigned int sindex; 988 1.8 christos bfd_vma stub_sec_addr = 989 1.1 christos (htab->stub_sec->output_section->vma + 990 1.1 christos htab->stub_sec->output_offset); 991 1.1 christos 992 1.1 christos for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++) 993 1.1 christos if (htab->amt_destination_addr[sindex] == srel) 994 1.1 christos return htab->amt_stub_offsets[sindex] + stub_sec_addr; 995 1.1 christos 996 1.1 christos /* Return an address that could not be reached by 16 bit relocs. */ 997 1.1 christos return 0x020000; 998 1.1 christos } 999 1.3 christos 1000 1.3 christos /* Perform a diff relocation. Nothing to do, as the difference value is already 1001 1.3 christos written into the section's contents. */ 1002 1.3 christos 1003 1.3 christos static bfd_reloc_status_type 1004 1.3 christos bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1005 1.8 christos arelent *reloc_entry ATTRIBUTE_UNUSED, 1006 1.8 christos asymbol *symbol ATTRIBUTE_UNUSED, 1007 1.8 christos void *data ATTRIBUTE_UNUSED, 1008 1.8 christos asection *input_section ATTRIBUTE_UNUSED, 1009 1.8 christos bfd *output_bfd ATTRIBUTE_UNUSED, 1010 1.3 christos char **error_message ATTRIBUTE_UNUSED) 1011 1.3 christos { 1012 1.3 christos return bfd_reloc_ok; 1013 1.3 christos } 1014 1.3 christos 1015 1.1 christos 1016 1.1 christos /* Perform a single relocation. By default we use the standard BFD 1017 1.1 christos routines, but a few relocs, we have to do them ourselves. */ 1018 1.1 christos 1019 1.8 christos static bfd_reloc_status_type 1020 1.8 christos avr_final_link_relocate (reloc_howto_type * howto, 1021 1.8 christos bfd * input_bfd, 1022 1.8 christos asection * input_section, 1023 1.8 christos bfd_byte * contents, 1024 1.8 christos Elf_Internal_Rela * rel, 1025 1.8 christos bfd_vma relocation, 1026 1.1 christos struct elf32_avr_link_hash_table * htab) 1027 1.1 christos { 1028 1.10 christos bfd_reloc_status_type r = bfd_reloc_ok; 1029 1.10 christos bfd_vma x; 1030 1.10 christos bfd_signed_vma srel; 1031 1.10 christos bfd_signed_vma reloc_addr; 1032 1.1 christos bool use_stubs = false; 1033 1.10 christos /* Usually is 0, unless we are generating code for a bootloader. */ 1034 1.1 christos bfd_signed_vma base_addr = htab->vector_base; 1035 1.1 christos 1036 1.1 christos /* Absolute addr of the reloc in the final excecutable. */ 1037 1.1 christos reloc_addr = rel->r_offset + input_section->output_section->vma 1038 1.1 christos + input_section->output_offset; 1039 1.1 christos 1040 1.1 christos switch (howto->type) 1041 1.1 christos { 1042 1.1 christos case R_AVR_7_PCREL: 1043 1.1 christos contents += rel->r_offset; 1044 1.1 christos srel = (bfd_signed_vma) relocation; 1045 1.1 christos srel += rel->r_addend; 1046 1.1 christos srel -= rel->r_offset; 1047 1.1 christos srel -= 2; /* Branch instructions add 2 to the PC... */ 1048 1.1 christos srel -= (input_section->output_section->vma + 1049 1.1 christos input_section->output_offset); 1050 1.1 christos 1051 1.10 christos if (srel & 1) 1052 1.1 christos return bfd_reloc_other; 1053 1.1 christos if (srel > ((1 << 7) - 1) || (srel < - (1 << 7))) 1054 1.1 christos return bfd_reloc_overflow; 1055 1.9 christos x = bfd_get_16 (input_bfd, contents); 1056 1.1 christos x = (x & 0xfc07) | (((srel >> 1) * 8) & 0x3f8); 1057 1.1 christos bfd_put_16 (input_bfd, x, contents); 1058 1.1 christos break; 1059 1.1 christos 1060 1.1 christos case R_AVR_13_PCREL: 1061 1.1 christos contents += rel->r_offset; 1062 1.1 christos srel = (bfd_signed_vma) relocation; 1063 1.1 christos srel += rel->r_addend; 1064 1.1 christos srel -= rel->r_offset; 1065 1.1 christos srel -= 2; /* Branch instructions add 2 to the PC... */ 1066 1.1 christos srel -= (input_section->output_section->vma + 1067 1.1 christos input_section->output_offset); 1068 1.1 christos 1069 1.10 christos if (srel & 1) 1070 1.1 christos return bfd_reloc_other; 1071 1.1 christos 1072 1.1 christos srel = avr_relative_distance_considering_wrap_around (srel); 1073 1.1 christos 1074 1.1 christos /* AVR addresses commands as words. */ 1075 1.1 christos srel >>= 1; 1076 1.1 christos 1077 1.1 christos /* Check for overflow. */ 1078 1.1 christos if (srel < -2048 || srel > 2047) 1079 1.8 christos { 1080 1.1 christos /* Relative distance is too large. */ 1081 1.1 christos 1082 1.1 christos /* Always apply WRAPAROUND for avr2, avr25, and avr4. */ 1083 1.1 christos switch (bfd_get_mach (input_bfd)) 1084 1.1 christos { 1085 1.1 christos case bfd_mach_avr2: 1086 1.1 christos case bfd_mach_avr25: 1087 1.1 christos case bfd_mach_avr4: 1088 1.1 christos break; 1089 1.1 christos 1090 1.1 christos default: 1091 1.1 christos return bfd_reloc_overflow; 1092 1.1 christos } 1093 1.1 christos } 1094 1.1 christos 1095 1.1 christos x = bfd_get_16 (input_bfd, contents); 1096 1.1 christos x = (x & 0xf000) | (srel & 0xfff); 1097 1.1 christos bfd_put_16 (input_bfd, x, contents); 1098 1.1 christos break; 1099 1.1 christos 1100 1.1 christos case R_AVR_LO8_LDI: 1101 1.1 christos contents += rel->r_offset; 1102 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1103 1.1 christos x = bfd_get_16 (input_bfd, contents); 1104 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1105 1.1 christos bfd_put_16 (input_bfd, x, contents); 1106 1.1 christos break; 1107 1.1 christos 1108 1.1 christos case R_AVR_LDI: 1109 1.1 christos contents += rel->r_offset; 1110 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1111 1.1 christos if (((srel > 0) && (srel & 0xffff) > 255) 1112 1.8 christos || ((srel < 0) && ((-srel) & 0xffff) > 128)) 1113 1.8 christos /* Remove offset for data/eeprom section. */ 1114 1.1 christos return bfd_reloc_overflow; 1115 1.1 christos 1116 1.1 christos x = bfd_get_16 (input_bfd, contents); 1117 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1118 1.1 christos bfd_put_16 (input_bfd, x, contents); 1119 1.1 christos break; 1120 1.1 christos 1121 1.1 christos case R_AVR_6: 1122 1.1 christos contents += rel->r_offset; 1123 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1124 1.1 christos if (((srel & 0xffff) > 63) || (srel < 0)) 1125 1.1 christos /* Remove offset for data/eeprom section. */ 1126 1.1 christos return bfd_reloc_overflow; 1127 1.1 christos x = bfd_get_16 (input_bfd, contents); 1128 1.8 christos x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7) 1129 1.1 christos | ((srel & (1 << 5)) << 8)); 1130 1.1 christos bfd_put_16 (input_bfd, x, contents); 1131 1.1 christos break; 1132 1.1 christos 1133 1.1 christos case R_AVR_6_ADIW: 1134 1.1 christos contents += rel->r_offset; 1135 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1136 1.1 christos if (((srel & 0xffff) > 63) || (srel < 0)) 1137 1.1 christos /* Remove offset for data/eeprom section. */ 1138 1.1 christos return bfd_reloc_overflow; 1139 1.1 christos x = bfd_get_16 (input_bfd, contents); 1140 1.1 christos x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2); 1141 1.1 christos bfd_put_16 (input_bfd, x, contents); 1142 1.1 christos break; 1143 1.1 christos 1144 1.1 christos case R_AVR_HI8_LDI: 1145 1.1 christos contents += rel->r_offset; 1146 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1147 1.1 christos srel = (srel >> 8) & 0xff; 1148 1.1 christos x = bfd_get_16 (input_bfd, contents); 1149 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1150 1.1 christos bfd_put_16 (input_bfd, x, contents); 1151 1.1 christos break; 1152 1.1 christos 1153 1.1 christos case R_AVR_HH8_LDI: 1154 1.1 christos contents += rel->r_offset; 1155 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1156 1.1 christos srel = (srel >> 16) & 0xff; 1157 1.1 christos x = bfd_get_16 (input_bfd, contents); 1158 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1159 1.1 christos bfd_put_16 (input_bfd, x, contents); 1160 1.1 christos break; 1161 1.1 christos 1162 1.1 christos case R_AVR_MS8_LDI: 1163 1.1 christos contents += rel->r_offset; 1164 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1165 1.1 christos srel = (srel >> 24) & 0xff; 1166 1.1 christos x = bfd_get_16 (input_bfd, contents); 1167 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1168 1.1 christos bfd_put_16 (input_bfd, x, contents); 1169 1.1 christos break; 1170 1.1 christos 1171 1.1 christos case R_AVR_LO8_LDI_NEG: 1172 1.1 christos contents += rel->r_offset; 1173 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1174 1.1 christos srel = -srel; 1175 1.1 christos x = bfd_get_16 (input_bfd, contents); 1176 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1177 1.1 christos bfd_put_16 (input_bfd, x, contents); 1178 1.1 christos break; 1179 1.1 christos 1180 1.1 christos case R_AVR_HI8_LDI_NEG: 1181 1.1 christos contents += rel->r_offset; 1182 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1183 1.1 christos srel = -srel; 1184 1.1 christos srel = (srel >> 8) & 0xff; 1185 1.1 christos x = bfd_get_16 (input_bfd, contents); 1186 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1187 1.1 christos bfd_put_16 (input_bfd, x, contents); 1188 1.1 christos break; 1189 1.1 christos 1190 1.1 christos case R_AVR_HH8_LDI_NEG: 1191 1.1 christos contents += rel->r_offset; 1192 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1193 1.1 christos srel = -srel; 1194 1.1 christos srel = (srel >> 16) & 0xff; 1195 1.1 christos x = bfd_get_16 (input_bfd, contents); 1196 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1197 1.1 christos bfd_put_16 (input_bfd, x, contents); 1198 1.1 christos break; 1199 1.1 christos 1200 1.1 christos case R_AVR_MS8_LDI_NEG: 1201 1.1 christos contents += rel->r_offset; 1202 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1203 1.1 christos srel = -srel; 1204 1.1 christos srel = (srel >> 24) & 0xff; 1205 1.1 christos x = bfd_get_16 (input_bfd, contents); 1206 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1207 1.1 christos bfd_put_16 (input_bfd, x, contents); 1208 1.1 christos break; 1209 1.1 christos 1210 1.1 christos case R_AVR_LO8_LDI_GS: 1211 1.1 christos use_stubs = (!htab->no_stubs); 1212 1.1 christos /* Fall through. */ 1213 1.1 christos case R_AVR_LO8_LDI_PM: 1214 1.1 christos contents += rel->r_offset; 1215 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1216 1.1 christos 1217 1.8 christos if (use_stubs 1218 1.8 christos && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1219 1.8 christos { 1220 1.8 christos bfd_vma old_srel = srel; 1221 1.8 christos 1222 1.8 christos /* We need to use the address of the stub instead. */ 1223 1.8 christos srel = avr_get_stub_addr (srel, htab); 1224 1.8 christos if (debug_stubs) 1225 1.8 christos printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1226 1.8 christos "reloc at address 0x%x.\n", 1227 1.8 christos (unsigned int) srel, 1228 1.8 christos (unsigned int) old_srel, 1229 1.1 christos (unsigned int) reloc_addr); 1230 1.1 christos 1231 1.10 christos if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1232 1.8 christos return bfd_reloc_overflow; 1233 1.1 christos } 1234 1.1 christos 1235 1.10 christos if (srel & 1) 1236 1.1 christos return bfd_reloc_other; 1237 1.1 christos srel = srel >> 1; 1238 1.1 christos x = bfd_get_16 (input_bfd, contents); 1239 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1240 1.1 christos bfd_put_16 (input_bfd, x, contents); 1241 1.1 christos break; 1242 1.1 christos 1243 1.1 christos case R_AVR_HI8_LDI_GS: 1244 1.1 christos use_stubs = (!htab->no_stubs); 1245 1.1 christos /* Fall through. */ 1246 1.1 christos case R_AVR_HI8_LDI_PM: 1247 1.1 christos contents += rel->r_offset; 1248 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1249 1.1 christos 1250 1.8 christos if (use_stubs 1251 1.8 christos && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1252 1.8 christos { 1253 1.8 christos bfd_vma old_srel = srel; 1254 1.8 christos 1255 1.8 christos /* We need to use the address of the stub instead. */ 1256 1.8 christos srel = avr_get_stub_addr (srel, htab); 1257 1.8 christos if (debug_stubs) 1258 1.8 christos printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1259 1.8 christos "reloc at address 0x%x.\n", 1260 1.8 christos (unsigned int) srel, 1261 1.8 christos (unsigned int) old_srel, 1262 1.1 christos (unsigned int) reloc_addr); 1263 1.1 christos 1264 1.10 christos if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1265 1.8 christos return bfd_reloc_overflow; 1266 1.1 christos } 1267 1.1 christos 1268 1.10 christos if (srel & 1) 1269 1.1 christos return bfd_reloc_other; 1270 1.1 christos srel = srel >> 1; 1271 1.1 christos srel = (srel >> 8) & 0xff; 1272 1.1 christos x = bfd_get_16 (input_bfd, contents); 1273 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1274 1.1 christos bfd_put_16 (input_bfd, x, contents); 1275 1.1 christos break; 1276 1.1 christos 1277 1.1 christos case R_AVR_HH8_LDI_PM: 1278 1.1 christos contents += rel->r_offset; 1279 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1280 1.10 christos if (srel & 1) 1281 1.1 christos return bfd_reloc_other; 1282 1.1 christos srel = srel >> 1; 1283 1.1 christos srel = (srel >> 16) & 0xff; 1284 1.1 christos x = bfd_get_16 (input_bfd, contents); 1285 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1286 1.1 christos bfd_put_16 (input_bfd, x, contents); 1287 1.1 christos break; 1288 1.1 christos 1289 1.1 christos case R_AVR_LO8_LDI_PM_NEG: 1290 1.1 christos contents += rel->r_offset; 1291 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1292 1.1 christos srel = -srel; 1293 1.10 christos if (srel & 1) 1294 1.1 christos return bfd_reloc_other; 1295 1.1 christos srel = srel >> 1; 1296 1.1 christos x = bfd_get_16 (input_bfd, contents); 1297 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1298 1.1 christos bfd_put_16 (input_bfd, x, contents); 1299 1.1 christos break; 1300 1.1 christos 1301 1.1 christos case R_AVR_HI8_LDI_PM_NEG: 1302 1.1 christos contents += rel->r_offset; 1303 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1304 1.1 christos srel = -srel; 1305 1.10 christos if (srel & 1) 1306 1.1 christos return bfd_reloc_other; 1307 1.1 christos srel = srel >> 1; 1308 1.1 christos srel = (srel >> 8) & 0xff; 1309 1.1 christos x = bfd_get_16 (input_bfd, contents); 1310 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1311 1.1 christos bfd_put_16 (input_bfd, x, contents); 1312 1.1 christos break; 1313 1.1 christos 1314 1.1 christos case R_AVR_HH8_LDI_PM_NEG: 1315 1.1 christos contents += rel->r_offset; 1316 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1317 1.1 christos srel = -srel; 1318 1.10 christos if (srel & 1) 1319 1.1 christos return bfd_reloc_other; 1320 1.1 christos srel = srel >> 1; 1321 1.1 christos srel = (srel >> 16) & 0xff; 1322 1.1 christos x = bfd_get_16 (input_bfd, contents); 1323 1.1 christos x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1324 1.1 christos bfd_put_16 (input_bfd, x, contents); 1325 1.1 christos break; 1326 1.1 christos 1327 1.1 christos case R_AVR_CALL: 1328 1.1 christos contents += rel->r_offset; 1329 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1330 1.10 christos if (srel & 1) 1331 1.1 christos return bfd_reloc_other; 1332 1.1 christos srel = srel >> 1; 1333 1.1 christos x = bfd_get_16 (input_bfd, contents); 1334 1.1 christos x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16; 1335 1.1 christos bfd_put_16 (input_bfd, x, contents); 1336 1.1 christos bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2); 1337 1.1 christos break; 1338 1.1 christos 1339 1.1 christos case R_AVR_16_PM: 1340 1.1 christos use_stubs = (!htab->no_stubs); 1341 1.1 christos contents += rel->r_offset; 1342 1.1 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1343 1.1 christos 1344 1.8 christos if (use_stubs 1345 1.8 christos && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1346 1.8 christos { 1347 1.8 christos bfd_vma old_srel = srel; 1348 1.8 christos 1349 1.8 christos /* We need to use the address of the stub instead. */ 1350 1.8 christos srel = avr_get_stub_addr (srel,htab); 1351 1.8 christos if (debug_stubs) 1352 1.8 christos printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1353 1.8 christos "reloc at address 0x%x.\n", 1354 1.8 christos (unsigned int) srel, 1355 1.8 christos (unsigned int) old_srel, 1356 1.1 christos (unsigned int) reloc_addr); 1357 1.1 christos 1358 1.10 christos if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1359 1.8 christos return bfd_reloc_overflow; 1360 1.1 christos } 1361 1.1 christos 1362 1.10 christos if (srel & 1) 1363 1.1 christos return bfd_reloc_other; 1364 1.1 christos srel = srel >> 1; 1365 1.1 christos bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents); 1366 1.1 christos break; 1367 1.3 christos 1368 1.3 christos case R_AVR_DIFF8: 1369 1.3 christos case R_AVR_DIFF16: 1370 1.3 christos case R_AVR_DIFF32: 1371 1.3 christos /* Nothing to do here, as contents already contains the diff value. */ 1372 1.3 christos r = bfd_reloc_ok; 1373 1.3 christos break; 1374 1.3 christos 1375 1.3 christos case R_AVR_LDS_STS_16: 1376 1.3 christos contents += rel->r_offset; 1377 1.3 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1378 1.10 christos if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf) 1379 1.3 christos return bfd_reloc_overflow; 1380 1.3 christos srel = srel & 0x7f; 1381 1.3 christos x = bfd_get_16 (input_bfd, contents); 1382 1.3 christos x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2); 1383 1.3 christos bfd_put_16 (input_bfd, x, contents); 1384 1.3 christos break; 1385 1.3 christos 1386 1.3 christos case R_AVR_PORT6: 1387 1.3 christos contents += rel->r_offset; 1388 1.3 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1389 1.10 christos if ((srel & 0xffff) > 0x3f) 1390 1.3 christos return bfd_reloc_overflow; 1391 1.3 christos x = bfd_get_16 (input_bfd, contents); 1392 1.3 christos x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f); 1393 1.3 christos bfd_put_16 (input_bfd, x, contents); 1394 1.3 christos break; 1395 1.3 christos 1396 1.3 christos case R_AVR_PORT5: 1397 1.3 christos contents += rel->r_offset; 1398 1.3 christos srel = (bfd_signed_vma) relocation + rel->r_addend; 1399 1.10 christos if ((srel & 0xffff) > 0x1f) 1400 1.3 christos return bfd_reloc_overflow; 1401 1.3 christos x = bfd_get_16 (input_bfd, contents); 1402 1.3 christos x = (x & 0xff07) | ((srel & 0x1f) << 3); 1403 1.3 christos bfd_put_16 (input_bfd, x, contents); 1404 1.3 christos break; 1405 1.1 christos 1406 1.1 christos default: 1407 1.1 christos r = _bfd_final_link_relocate (howto, input_bfd, input_section, 1408 1.1 christos contents, rel->r_offset, 1409 1.1 christos relocation, rel->r_addend); 1410 1.1 christos } 1411 1.1 christos 1412 1.1 christos return r; 1413 1.1 christos } 1414 1.1 christos 1415 1.1 christos /* Relocate an AVR ELF section. */ 1416 1.10 christos 1417 1.1 christos static int 1418 1.1 christos elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED, 1419 1.1 christos struct bfd_link_info *info, 1420 1.1 christos bfd *input_bfd, 1421 1.1 christos asection *input_section, 1422 1.1 christos bfd_byte *contents, 1423 1.1 christos Elf_Internal_Rela *relocs, 1424 1.1 christos Elf_Internal_Sym *local_syms, 1425 1.1 christos asection **local_sections) 1426 1.8 christos { 1427 1.1 christos Elf_Internal_Shdr * symtab_hdr; 1428 1.8 christos struct elf_link_hash_entry ** sym_hashes; 1429 1.8 christos Elf_Internal_Rela * rel; 1430 1.1 christos Elf_Internal_Rela * relend; 1431 1.1 christos struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info); 1432 1.1 christos 1433 1.10 christos if (htab == NULL) 1434 1.1 christos return false; 1435 1.1 christos 1436 1.1 christos symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 1437 1.1 christos sym_hashes = elf_sym_hashes (input_bfd); 1438 1.1 christos relend = relocs + input_section->reloc_count; 1439 1.1 christos 1440 1.1 christos for (rel = relocs; rel < relend; rel ++) 1441 1.8 christos { 1442 1.8 christos reloc_howto_type * howto; 1443 1.8 christos unsigned long r_symndx; 1444 1.8 christos Elf_Internal_Sym * sym; 1445 1.1 christos asection * sec; 1446 1.8 christos struct elf_link_hash_entry * h; 1447 1.8 christos bfd_vma relocation; 1448 1.8 christos bfd_reloc_status_type r; 1449 1.8 christos const char * name; 1450 1.1 christos int r_type; 1451 1.1 christos 1452 1.1 christos r_type = ELF32_R_TYPE (rel->r_info); 1453 1.1 christos r_symndx = ELF32_R_SYM (rel->r_info); 1454 1.1 christos howto = elf_avr_howto_table + r_type; 1455 1.1 christos h = NULL; 1456 1.1 christos sym = NULL; 1457 1.1 christos sec = NULL; 1458 1.1 christos 1459 1.1 christos if (r_symndx < symtab_hdr->sh_info) 1460 1.1 christos { 1461 1.1 christos sym = local_syms + r_symndx; 1462 1.1 christos sec = local_sections [r_symndx]; 1463 1.1 christos relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 1464 1.1 christos 1465 1.1 christos name = bfd_elf_string_from_elf_section 1466 1.9 christos (input_bfd, symtab_hdr->sh_link, sym->st_name); 1467 1.1 christos name = name == NULL ? bfd_section_name (sec) : name; 1468 1.1 christos } 1469 1.1 christos else 1470 1.10 christos { 1471 1.1 christos bool unresolved_reloc, warned, ignored; 1472 1.1 christos 1473 1.1 christos RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 1474 1.1 christos r_symndx, symtab_hdr, sym_hashes, 1475 1.1 christos h, sec, relocation, 1476 1.1 christos unresolved_reloc, warned, ignored); 1477 1.1 christos 1478 1.1 christos name = h->root.root.string; 1479 1.1 christos } 1480 1.1 christos 1481 1.1 christos if (sec != NULL && discarded_section (sec)) 1482 1.1 christos RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 1483 1.1 christos rel, 1, relend, howto, 0, contents); 1484 1.6 christos 1485 1.1 christos if (bfd_link_relocatable (info)) 1486 1.1 christos continue; 1487 1.1 christos 1488 1.1 christos r = avr_final_link_relocate (howto, input_bfd, input_section, 1489 1.1 christos contents, rel, relocation, htab); 1490 1.1 christos 1491 1.1 christos if (r != bfd_reloc_ok) 1492 1.1 christos { 1493 1.1 christos switch (r) 1494 1.1 christos { 1495 1.6 christos case bfd_reloc_overflow: 1496 1.6 christos (*info->callbacks->reloc_overflow) 1497 1.6 christos (info, (h ? &h->root : NULL), name, howto->name, 1498 1.1 christos (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 1499 1.1 christos break; 1500 1.1 christos 1501 1.6 christos case bfd_reloc_undefined: 1502 1.10 christos (*info->callbacks->undefined_symbol) 1503 1.1 christos (info, name, input_bfd, input_section, rel->r_offset, true); 1504 1.1 christos break; 1505 1.1 christos 1506 1.10 christos case bfd_reloc_outofrange: 1507 1.10 christos /* xgettext:c-format */ 1508 1.10 christos (*info->callbacks->einfo) 1509 1.10 christos (_("%X%H: %s against `%s':" 1510 1.10 christos " error: relocation applies outside section\n"), 1511 1.1 christos input_bfd, input_section, rel->r_offset, howto->name, name); 1512 1.1 christos break; 1513 1.10 christos 1514 1.10 christos case bfd_reloc_other: 1515 1.10 christos /* xgettext:c-format */ 1516 1.10 christos (*info->callbacks->einfo) 1517 1.10 christos (_("%X%H: %s against `%s':" 1518 1.10 christos " error: relocation target address is odd\n"), 1519 1.1 christos input_bfd, input_section, rel->r_offset, howto->name, name); 1520 1.1 christos break; 1521 1.1 christos 1522 1.10 christos default: 1523 1.10 christos /* xgettext:c-format */ 1524 1.10 christos (*info->callbacks->einfo) 1525 1.10 christos (_("%X%H: %s against `%s':" 1526 1.10 christos " internal error: unexpected relocation result %d\n"), 1527 1.1 christos input_bfd, input_section, rel->r_offset, howto->name, name, r); 1528 1.1 christos break; 1529 1.1 christos } 1530 1.1 christos } 1531 1.1 christos } 1532 1.10 christos 1533 1.1 christos return true; 1534 1.1 christos } 1535 1.1 christos 1536 1.1 christos /* The final processing done just before writing out a AVR ELF object 1537 1.1 christos file. This gets the AVR architecture right based on the machine 1538 1.1 christos number. */ 1539 1.10 christos 1540 1.9 christos static bool 1541 1.1 christos bfd_elf_avr_final_write_processing (bfd *abfd) 1542 1.1 christos { 1543 1.1 christos unsigned long val; 1544 1.1 christos 1545 1.1 christos switch (bfd_get_mach (abfd)) 1546 1.1 christos { 1547 1.1 christos default: 1548 1.1 christos case bfd_mach_avr2: 1549 1.1 christos val = E_AVR_MACH_AVR2; 1550 1.1 christos break; 1551 1.1 christos 1552 1.1 christos case bfd_mach_avr1: 1553 1.1 christos val = E_AVR_MACH_AVR1; 1554 1.1 christos break; 1555 1.1 christos 1556 1.1 christos case bfd_mach_avr25: 1557 1.1 christos val = E_AVR_MACH_AVR25; 1558 1.1 christos break; 1559 1.1 christos 1560 1.1 christos case bfd_mach_avr3: 1561 1.1 christos val = E_AVR_MACH_AVR3; 1562 1.1 christos break; 1563 1.1 christos 1564 1.1 christos case bfd_mach_avr31: 1565 1.1 christos val = E_AVR_MACH_AVR31; 1566 1.1 christos break; 1567 1.1 christos 1568 1.1 christos case bfd_mach_avr35: 1569 1.1 christos val = E_AVR_MACH_AVR35; 1570 1.1 christos break; 1571 1.1 christos 1572 1.1 christos case bfd_mach_avr4: 1573 1.1 christos val = E_AVR_MACH_AVR4; 1574 1.1 christos break; 1575 1.1 christos 1576 1.1 christos case bfd_mach_avr5: 1577 1.1 christos val = E_AVR_MACH_AVR5; 1578 1.1 christos break; 1579 1.1 christos 1580 1.1 christos case bfd_mach_avr51: 1581 1.1 christos val = E_AVR_MACH_AVR51; 1582 1.1 christos break; 1583 1.1 christos 1584 1.1 christos case bfd_mach_avr6: 1585 1.1 christos val = E_AVR_MACH_AVR6; 1586 1.1 christos break; 1587 1.1 christos 1588 1.1 christos case bfd_mach_avrxmega1: 1589 1.1 christos val = E_AVR_MACH_XMEGA1; 1590 1.1 christos break; 1591 1.1 christos 1592 1.1 christos case bfd_mach_avrxmega2: 1593 1.1 christos val = E_AVR_MACH_XMEGA2; 1594 1.1 christos break; 1595 1.1 christos 1596 1.1 christos case bfd_mach_avrxmega3: 1597 1.1 christos val = E_AVR_MACH_XMEGA3; 1598 1.1 christos break; 1599 1.1 christos 1600 1.1 christos case bfd_mach_avrxmega4: 1601 1.1 christos val = E_AVR_MACH_XMEGA4; 1602 1.1 christos break; 1603 1.1 christos 1604 1.1 christos case bfd_mach_avrxmega5: 1605 1.1 christos val = E_AVR_MACH_XMEGA5; 1606 1.1 christos break; 1607 1.1 christos 1608 1.1 christos case bfd_mach_avrxmega6: 1609 1.1 christos val = E_AVR_MACH_XMEGA6; 1610 1.1 christos break; 1611 1.1 christos 1612 1.1 christos case bfd_mach_avrxmega7: 1613 1.1 christos val = E_AVR_MACH_XMEGA7; 1614 1.3 christos break; 1615 1.3 christos 1616 1.3 christos case bfd_mach_avrtiny: 1617 1.3 christos val = E_AVR_MACH_AVRTINY; 1618 1.1 christos break; 1619 1.1 christos } 1620 1.1 christos 1621 1.1 christos elf_elfheader (abfd)->e_machine = EM_AVR; 1622 1.1 christos elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH; 1623 1.9 christos elf_elfheader (abfd)->e_flags |= val; 1624 1.1 christos return _bfd_elf_final_write_processing (abfd); 1625 1.1 christos } 1626 1.1 christos 1627 1.1 christos /* Set the right machine number. */ 1628 1.10 christos 1629 1.1 christos static bool 1630 1.1 christos elf32_avr_object_p (bfd *abfd) 1631 1.1 christos { 1632 1.1 christos unsigned int e_set = bfd_mach_avr2; 1633 1.1 christos 1634 1.1 christos if (elf_elfheader (abfd)->e_machine == EM_AVR 1635 1.1 christos || elf_elfheader (abfd)->e_machine == EM_AVR_OLD) 1636 1.1 christos { 1637 1.1 christos int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH; 1638 1.1 christos 1639 1.1 christos switch (e_mach) 1640 1.1 christos { 1641 1.1 christos default: 1642 1.1 christos case E_AVR_MACH_AVR2: 1643 1.1 christos e_set = bfd_mach_avr2; 1644 1.1 christos break; 1645 1.1 christos 1646 1.1 christos case E_AVR_MACH_AVR1: 1647 1.1 christos e_set = bfd_mach_avr1; 1648 1.1 christos break; 1649 1.1 christos 1650 1.1 christos case E_AVR_MACH_AVR25: 1651 1.1 christos e_set = bfd_mach_avr25; 1652 1.1 christos break; 1653 1.1 christos 1654 1.1 christos case E_AVR_MACH_AVR3: 1655 1.1 christos e_set = bfd_mach_avr3; 1656 1.1 christos break; 1657 1.1 christos 1658 1.1 christos case E_AVR_MACH_AVR31: 1659 1.1 christos e_set = bfd_mach_avr31; 1660 1.1 christos break; 1661 1.1 christos 1662 1.1 christos case E_AVR_MACH_AVR35: 1663 1.1 christos e_set = bfd_mach_avr35; 1664 1.1 christos break; 1665 1.1 christos 1666 1.1 christos case E_AVR_MACH_AVR4: 1667 1.1 christos e_set = bfd_mach_avr4; 1668 1.1 christos break; 1669 1.1 christos 1670 1.1 christos case E_AVR_MACH_AVR5: 1671 1.1 christos e_set = bfd_mach_avr5; 1672 1.1 christos break; 1673 1.1 christos 1674 1.1 christos case E_AVR_MACH_AVR51: 1675 1.1 christos e_set = bfd_mach_avr51; 1676 1.1 christos break; 1677 1.1 christos 1678 1.1 christos case E_AVR_MACH_AVR6: 1679 1.1 christos e_set = bfd_mach_avr6; 1680 1.1 christos break; 1681 1.1 christos 1682 1.1 christos case E_AVR_MACH_XMEGA1: 1683 1.1 christos e_set = bfd_mach_avrxmega1; 1684 1.1 christos break; 1685 1.1 christos 1686 1.1 christos case E_AVR_MACH_XMEGA2: 1687 1.1 christos e_set = bfd_mach_avrxmega2; 1688 1.1 christos break; 1689 1.1 christos 1690 1.1 christos case E_AVR_MACH_XMEGA3: 1691 1.1 christos e_set = bfd_mach_avrxmega3; 1692 1.1 christos break; 1693 1.1 christos 1694 1.1 christos case E_AVR_MACH_XMEGA4: 1695 1.1 christos e_set = bfd_mach_avrxmega4; 1696 1.1 christos break; 1697 1.1 christos 1698 1.1 christos case E_AVR_MACH_XMEGA5: 1699 1.1 christos e_set = bfd_mach_avrxmega5; 1700 1.1 christos break; 1701 1.1 christos 1702 1.1 christos case E_AVR_MACH_XMEGA6: 1703 1.1 christos e_set = bfd_mach_avrxmega6; 1704 1.1 christos break; 1705 1.1 christos 1706 1.1 christos case E_AVR_MACH_XMEGA7: 1707 1.1 christos e_set = bfd_mach_avrxmega7; 1708 1.3 christos break; 1709 1.3 christos 1710 1.3 christos case E_AVR_MACH_AVRTINY: 1711 1.3 christos e_set = bfd_mach_avrtiny; 1712 1.1 christos break; 1713 1.1 christos } 1714 1.1 christos } 1715 1.1 christos return bfd_default_set_arch_mach (abfd, bfd_arch_avr, 1716 1.1 christos e_set); 1717 1.1 christos } 1718 1.3 christos 1719 1.3 christos /* Returns whether the relocation type passed is a diff reloc. */ 1720 1.10 christos 1721 1.3 christos static bool 1722 1.3 christos elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel) 1723 1.3 christos { 1724 1.8 christos return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8 1725 1.8 christos ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16 1726 1.3 christos || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32); 1727 1.3 christos } 1728 1.3 christos 1729 1.3 christos /* Reduce the diff value written in the section by count if the shrinked 1730 1.3 christos insn address happens to fall between the two symbols for which this 1731 1.3 christos diff reloc was emitted. */ 1732 1.3 christos 1733 1.3 christos static void 1734 1.8 christos elf32_avr_adjust_diff_reloc_value (bfd *abfd, 1735 1.8 christos struct bfd_section *isec, 1736 1.8 christos Elf_Internal_Rela *irel, 1737 1.8 christos bfd_vma symval, 1738 1.8 christos bfd_vma shrinked_insn_address, 1739 1.3 christos int count) 1740 1.3 christos { 1741 1.3 christos unsigned char *reloc_contents = NULL; 1742 1.3 christos unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents; 1743 1.3 christos if (isec_contents == NULL) 1744 1.3 christos { 1745 1.3 christos if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents)) 1746 1.3 christos return; 1747 1.3 christos 1748 1.3 christos elf_section_data (isec)->this_hdr.contents = isec_contents; 1749 1.3 christos } 1750 1.3 christos 1751 1.3 christos reloc_contents = isec_contents + irel->r_offset; 1752 1.3 christos 1753 1.7 christos /* Read value written in object file. */ 1754 1.3 christos bfd_signed_vma x = 0; 1755 1.3 christos switch (ELF32_R_TYPE (irel->r_info)) 1756 1.3 christos { 1757 1.3 christos case R_AVR_DIFF8: 1758 1.7 christos { 1759 1.3 christos x = bfd_get_signed_8 (abfd, reloc_contents); 1760 1.3 christos break; 1761 1.3 christos } 1762 1.3 christos case R_AVR_DIFF16: 1763 1.7 christos { 1764 1.3 christos x = bfd_get_signed_16 (abfd, reloc_contents); 1765 1.3 christos break; 1766 1.3 christos } 1767 1.3 christos case R_AVR_DIFF32: 1768 1.7 christos { 1769 1.3 christos x = bfd_get_signed_32 (abfd, reloc_contents); 1770 1.3 christos break; 1771 1.3 christos } 1772 1.3 christos default: 1773 1.3 christos { 1774 1.3 christos BFD_FAIL(); 1775 1.3 christos } 1776 1.3 christos } 1777 1.3 christos 1778 1.3 christos /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written 1779 1.3 christos into the object file at the reloc offset. sym2's logical value is 1780 1.3 christos symval (<start_of_section>) + reloc addend. Compute the start and end 1781 1.3 christos addresses and check if the shrinked insn falls between sym1 and sym2. */ 1782 1.7 christos 1783 1.7 christos bfd_vma sym2_address = symval + irel->r_addend; 1784 1.7 christos bfd_vma sym1_address = sym2_address - x; 1785 1.7 christos 1786 1.7 christos /* Don't assume sym2 is bigger than sym1 - the difference 1787 1.7 christos could be negative. Compute start and end addresses, and 1788 1.7 christos use those to see if they span shrinked_insn_address. */ 1789 1.7 christos 1790 1.7 christos bfd_vma start_address = sym1_address < sym2_address 1791 1.7 christos ? sym1_address : sym2_address; 1792 1.7 christos bfd_vma end_address = sym1_address > sym2_address 1793 1.3 christos ? sym1_address : sym2_address; 1794 1.3 christos 1795 1.3 christos 1796 1.8 christos if (shrinked_insn_address >= start_address 1797 1.3 christos && shrinked_insn_address < end_address) 1798 1.7 christos { 1799 1.7 christos /* Reduce the diff value by count bytes and write it back into section 1800 1.7 christos contents. */ 1801 1.7 christos bfd_signed_vma new_diff = x < 0 ? x + count : x - count; 1802 1.8 christos 1803 1.8 christos if (sym2_address > shrinked_insn_address) 1804 1.8 christos irel->r_addend -= count; 1805 1.3 christos 1806 1.3 christos switch (ELF32_R_TYPE (irel->r_info)) 1807 1.3 christos { 1808 1.3 christos case R_AVR_DIFF8: 1809 1.8 christos { 1810 1.8 christos bfd_put_signed_8 (abfd, new_diff, reloc_contents); 1811 1.3 christos break; 1812 1.3 christos } 1813 1.3 christos case R_AVR_DIFF16: 1814 1.8 christos { 1815 1.8 christos bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents); 1816 1.3 christos break; 1817 1.3 christos } 1818 1.3 christos case R_AVR_DIFF32: 1819 1.8 christos { 1820 1.8 christos bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents); 1821 1.3 christos break; 1822 1.3 christos } 1823 1.3 christos default: 1824 1.8 christos { 1825 1.3 christos BFD_FAIL(); 1826 1.3 christos } 1827 1.3 christos } 1828 1.3 christos 1829 1.3 christos } 1830 1.1 christos } 1831 1.7 christos 1832 1.7 christos static void 1833 1.8 christos elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd, 1834 1.8 christos asection *isec, 1835 1.8 christos Elf_Internal_Rela *irel, bfd_vma symval, 1836 1.8 christos bfd_vma shrinked_insn_address, 1837 1.8 christos bfd_vma shrink_boundary, 1838 1.7 christos int count) 1839 1.7 christos { 1840 1.7 christos 1841 1.7 christos if (elf32_avr_is_diff_reloc (irel)) 1842 1.7 christos { 1843 1.8 christos elf32_avr_adjust_diff_reloc_value (abfd, isec, irel, 1844 1.8 christos symval, 1845 1.8 christos shrinked_insn_address, 1846 1.7 christos count); 1847 1.7 christos } 1848 1.7 christos else 1849 1.7 christos { 1850 1.10 christos bfd_vma reloc_value = symval + irel->r_addend; 1851 1.7 christos bool addend_within_shrink_boundary = reloc_value <= shrink_boundary; 1852 1.10 christos 1853 1.8 christos bool reloc_spans_insn = 1854 1.8 christos (symval <= shrinked_insn_address 1855 1.8 christos && reloc_value > shrinked_insn_address 1856 1.7 christos && addend_within_shrink_boundary); 1857 1.7 christos 1858 1.8 christos if (! reloc_spans_insn) 1859 1.7 christos return; 1860 1.7 christos 1861 1.7 christos irel->r_addend -= count; 1862 1.7 christos 1863 1.8 christos if (debug_relax) 1864 1.7 christos printf ("Relocation's addend needed to be fixed \n"); 1865 1.7 christos } 1866 1.7 christos } 1867 1.10 christos 1868 1.8 christos static bool 1869 1.8 christos avr_should_move_sym (symvalue symval, 1870 1.8 christos bfd_vma start, 1871 1.10 christos bfd_vma end, 1872 1.8 christos bool did_pad) 1873 1.10 christos { 1874 1.8 christos bool sym_within_boundary = did_pad ? symval < end : symval <= end; 1875 1.8 christos return (symval > start && sym_within_boundary); 1876 1.8 christos } 1877 1.10 christos 1878 1.8 christos static bool 1879 1.8 christos avr_should_reduce_sym_size (symvalue symval, 1880 1.8 christos symvalue symend, 1881 1.8 christos bfd_vma start, 1882 1.10 christos bfd_vma end, 1883 1.8 christos bool did_pad) 1884 1.10 christos { 1885 1.8 christos bool sym_end_within_boundary = did_pad ? symend < end : symend <= end; 1886 1.8 christos return (symval <= start && symend > start && sym_end_within_boundary); 1887 1.8 christos } 1888 1.10 christos 1889 1.8 christos static bool 1890 1.8 christos avr_should_increase_sym_size (symvalue symval, 1891 1.8 christos symvalue symend, 1892 1.8 christos bfd_vma start, 1893 1.10 christos bfd_vma end, 1894 1.8 christos bool did_pad) 1895 1.10 christos { 1896 1.10 christos return (avr_should_move_sym (symval, start, end, did_pad) 1897 1.8 christos && symend >= end && did_pad); 1898 1.8 christos } 1899 1.1 christos 1900 1.1 christos /* Delete some bytes from a section while changing the size of an instruction. 1901 1.1 christos The parameter "addr" denotes the section-relative offset pointing just 1902 1.7 christos behind the shrinked instruction. "addr+count" point at the first 1903 1.7 christos byte just behind the original unshrinked instruction. If delete_shrinks_insn 1904 1.7 christos is FALSE, we are deleting redundant padding bytes from relax_info prop 1905 1.7 christos record handling. In that case, addr is section-relative offset of start 1906 1.1 christos of padding, and count is the number of padding bytes to delete. */ 1907 1.10 christos 1908 1.1 christos static bool 1909 1.8 christos elf32_avr_relax_delete_bytes (bfd *abfd, 1910 1.8 christos asection *sec, 1911 1.8 christos bfd_vma addr, 1912 1.10 christos int count, 1913 1.1 christos bool delete_shrinks_insn) 1914 1.1 christos { 1915 1.1 christos Elf_Internal_Shdr *symtab_hdr; 1916 1.1 christos unsigned int sec_shndx; 1917 1.1 christos bfd_byte *contents; 1918 1.1 christos Elf_Internal_Rela *irel, *irelend; 1919 1.1 christos Elf_Internal_Sym *isym; 1920 1.8 christos Elf_Internal_Sym *isymbuf = NULL; 1921 1.1 christos bfd_vma toaddr; 1922 1.1 christos struct elf_link_hash_entry **sym_hashes; 1923 1.1 christos struct elf_link_hash_entry **end_hashes; 1924 1.5 christos unsigned int symcount; 1925 1.5 christos struct avr_relax_info *relax_info; 1926 1.10 christos struct avr_property_record *prop_record = NULL; 1927 1.10 christos bool did_shrink = false; 1928 1.1 christos bool did_pad = false; 1929 1.1 christos 1930 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1931 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 1932 1.5 christos contents = elf_section_data (sec)->this_hdr.contents; 1933 1.1 christos relax_info = get_avr_relax_info (sec); 1934 1.1 christos 1935 1.1 christos toaddr = sec->size; 1936 1.5 christos 1937 1.5 christos if (relax_info->records.count > 0) 1938 1.5 christos { 1939 1.8 christos /* There should be no property record within the range of deleted 1940 1.8 christos bytes, however, there might be a property record for ADDR, this is 1941 1.8 christos how we handle alignment directives. 1942 1.5 christos Find the next (if any) property record after the deleted bytes. */ 1943 1.5 christos unsigned int i; 1944 1.5 christos 1945 1.8 christos for (i = 0; i < relax_info->records.count; ++i) 1946 1.8 christos { 1947 1.5 christos bfd_vma offset = relax_info->records.items [i].offset; 1948 1.8 christos 1949 1.8 christos BFD_ASSERT (offset <= addr || offset >= (addr + count)); 1950 1.8 christos if (offset >= (addr + count)) 1951 1.8 christos { 1952 1.8 christos prop_record = &relax_info->records.items [i]; 1953 1.8 christos toaddr = offset; 1954 1.8 christos break; 1955 1.8 christos } 1956 1.8 christos } 1957 1.6 christos } 1958 1.1 christos 1959 1.1 christos irel = elf_section_data (sec)->relocs; 1960 1.1 christos irelend = irel + sec->reloc_count; 1961 1.1 christos 1962 1.1 christos /* Actually delete the bytes. */ 1963 1.6 christos if (toaddr - addr - count > 0) 1964 1.6 christos { 1965 1.8 christos memmove (contents + addr, contents + addr + count, 1966 1.10 christos (size_t) (toaddr - addr - count)); 1967 1.6 christos did_shrink = true; 1968 1.5 christos } 1969 1.6 christos if (prop_record == NULL) 1970 1.6 christos { 1971 1.10 christos sec->size -= count; 1972 1.6 christos did_shrink = true; 1973 1.5 christos } 1974 1.5 christos else 1975 1.5 christos { 1976 1.5 christos /* Use the property record to fill in the bytes we've opened up. */ 1977 1.5 christos int fill = 0; 1978 1.8 christos switch (prop_record->type) 1979 1.8 christos { 1980 1.8 christos case RECORD_ORG_AND_FILL: 1981 1.8 christos fill = prop_record->data.org.fill; 1982 1.8 christos /* Fall through. */ 1983 1.8 christos case RECORD_ORG: 1984 1.8 christos break; 1985 1.8 christos case RECORD_ALIGN_AND_FILL: 1986 1.8 christos fill = prop_record->data.align.fill; 1987 1.8 christos /* Fall through. */ 1988 1.8 christos case RECORD_ALIGN: 1989 1.8 christos prop_record->data.align.preceding_deleted += count; 1990 1.8 christos break; 1991 1.6 christos }; 1992 1.8 christos /* If toaddr == (addr + count), then we didn't delete anything, yet 1993 1.8 christos we fill count bytes backwards from toaddr. This is still ok - we 1994 1.8 christos end up overwriting the bytes we would have deleted. We just need 1995 1.8 christos to remember we didn't delete anything i.e. don't set did_shrink, 1996 1.5 christos so that we don't corrupt reloc offsets or symbol values.*/ 1997 1.10 christos memset (contents + toaddr - count, fill, count); 1998 1.5 christos did_pad = true; 1999 1.1 christos } 2000 1.6 christos 2001 1.10 christos if (!did_shrink) 2002 1.6 christos return true; 2003 1.1 christos 2004 1.1 christos /* Adjust all the reloc addresses. */ 2005 1.1 christos for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) 2006 1.1 christos { 2007 1.1 christos bfd_vma old_reloc_address; 2008 1.1 christos 2009 1.8 christos old_reloc_address = (sec->output_section->vma 2010 1.1 christos + sec->output_offset + irel->r_offset); 2011 1.1 christos 2012 1.1 christos /* Get the new reloc address. */ 2013 1.8 christos if ((irel->r_offset > addr 2014 1.8 christos && irel->r_offset < toaddr)) 2015 1.8 christos { 2016 1.8 christos if (debug_relax) 2017 1.8 christos printf ("Relocation at address 0x%x needs to be moved.\n" 2018 1.8 christos "Old section offset: 0x%x, New section offset: 0x%x \n", 2019 1.8 christos (unsigned int) old_reloc_address, 2020 1.8 christos (unsigned int) irel->r_offset, 2021 1.1 christos (unsigned int) ((irel->r_offset) - count)); 2022 1.8 christos 2023 1.8 christos irel->r_offset -= count; 2024 1.1 christos } 2025 1.1 christos 2026 1.1 christos } 2027 1.1 christos 2028 1.1 christos /* The reloc's own addresses are now ok. However, we need to readjust 2029 1.1 christos the reloc's addend, i.e. the reloc's value if two conditions are met: 2030 1.8 christos 1.) the reloc is relative to a symbol in this section that 2031 1.1 christos is located in front of the shrinked instruction 2032 1.1 christos 2.) symbol plus addend end up behind the shrinked instruction. 2033 1.1 christos 2034 1.1 christos The most common case where this happens are relocs relative to 2035 1.1 christos the section-start symbol. 2036 1.1 christos 2037 1.1 christos This step needs to be done for all of the sections of the bfd. */ 2038 1.1 christos 2039 1.1 christos { 2040 1.1 christos struct bfd_section *isec; 2041 1.1 christos 2042 1.1 christos for (isec = abfd->sections; isec; isec = isec->next) 2043 1.1 christos { 2044 1.1 christos bfd_vma symval; 2045 1.1 christos bfd_vma shrinked_insn_address; 2046 1.1 christos 2047 1.1 christos if (isec->reloc_count == 0) 2048 1.1 christos continue; 2049 1.1 christos 2050 1.8 christos shrinked_insn_address = (sec->output_section->vma 2051 1.7 christos + sec->output_offset + addr); 2052 1.8 christos if (delete_shrinks_insn) 2053 1.1 christos shrinked_insn_address -= count; 2054 1.1 christos 2055 1.1 christos irel = elf_section_data (isec)->relocs; 2056 1.1 christos /* PR 12161: Read in the relocs for this section if necessary. */ 2057 1.10 christos if (irel == NULL) 2058 1.1 christos irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true); 2059 1.1 christos 2060 1.8 christos for (irelend = irel + isec->reloc_count; 2061 1.8 christos irel < irelend; 2062 1.8 christos irel++) 2063 1.8 christos { 2064 1.8 christos /* Read this BFD's local symbols if we haven't done 2065 1.8 christos so already. */ 2066 1.8 christos if (isymbuf == NULL && symtab_hdr->sh_info != 0) 2067 1.8 christos { 2068 1.8 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2069 1.8 christos if (isymbuf == NULL) 2070 1.8 christos isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 2071 1.8 christos symtab_hdr->sh_info, 0, 2072 1.8 christos NULL, NULL, NULL); 2073 1.10 christos if (isymbuf == NULL) 2074 1.8 christos return false; 2075 1.8 christos } 2076 1.8 christos 2077 1.8 christos /* Get the value of the symbol referred to by the reloc. */ 2078 1.8 christos if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 2079 1.8 christos { 2080 1.8 christos /* A local symbol. */ 2081 1.8 christos asection *sym_sec; 2082 1.8 christos 2083 1.8 christos isym = isymbuf + ELF32_R_SYM (irel->r_info); 2084 1.8 christos sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 2085 1.8 christos symval = isym->st_value; 2086 1.8 christos /* If the reloc is absolute, it will not have 2087 1.8 christos a symbol or section associated with it. */ 2088 1.8 christos if (sym_sec == sec) 2089 1.8 christos { 2090 1.8 christos /* If there is an alignment boundary, we only need to 2091 1.8 christos adjust addends that end up below the boundary. */ 2092 1.8 christos bfd_vma shrink_boundary = (toaddr 2093 1.8 christos + sec->output_section->vma 2094 1.8 christos + sec->output_offset); 2095 1.8 christos 2096 1.8 christos symval += sym_sec->output_section->vma 2097 1.8 christos + sym_sec->output_offset; 2098 1.8 christos 2099 1.8 christos if (debug_relax) 2100 1.8 christos printf ("Checking if the relocation's " 2101 1.8 christos "addend needs corrections.\n" 2102 1.8 christos "Address of anchor symbol: 0x%x \n" 2103 1.8 christos "Address of relocation target: 0x%x \n" 2104 1.8 christos "Address of relaxed insn: 0x%x \n", 2105 1.8 christos (unsigned int) symval, 2106 1.8 christos (unsigned int) (symval + irel->r_addend), 2107 1.8 christos (unsigned int) shrinked_insn_address); 2108 1.8 christos 2109 1.8 christos elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel, 2110 1.8 christos symval, 2111 1.8 christos shrinked_insn_address, 2112 1.8 christos shrink_boundary, 2113 1.8 christos count); 2114 1.1 christos } 2115 1.1 christos /* else...Reference symbol is absolute. No adjustment needed. */ 2116 1.1 christos } 2117 1.1 christos /* else...Reference symbol is extern. No need for adjusting 2118 1.1 christos the addend. */ 2119 1.1 christos } 2120 1.1 christos } 2121 1.1 christos } 2122 1.1 christos 2123 1.1 christos /* Adjust the local symbols defined in this section. */ 2124 1.1 christos isym = (Elf_Internal_Sym *) symtab_hdr->contents; 2125 1.1 christos /* Fix PR 9841, there may be no local symbols. */ 2126 1.1 christos if (isym != NULL) 2127 1.1 christos { 2128 1.1 christos Elf_Internal_Sym *isymend; 2129 1.1 christos 2130 1.1 christos isymend = isym + symtab_hdr->sh_info; 2131 1.1 christos for (; isym < isymend; isym++) 2132 1.3 christos { 2133 1.8 christos if (isym->st_shndx == sec_shndx) 2134 1.8 christos { 2135 1.8 christos symvalue symval = isym->st_value; 2136 1.8 christos symvalue symend = symval + isym->st_size; 2137 1.8 christos if (avr_should_reduce_sym_size (symval, symend, 2138 1.8 christos addr, toaddr, did_pad)) 2139 1.8 christos { 2140 1.8 christos /* If this assert fires then we have a symbol that ends 2141 1.8 christos part way through an instruction. Does that make 2142 1.8 christos sense? */ 2143 1.8 christos BFD_ASSERT (isym->st_value + isym->st_size >= addr + count); 2144 1.8 christos isym->st_size -= count; 2145 1.8 christos } 2146 1.8 christos else if (avr_should_increase_sym_size (symval, symend, 2147 1.8 christos addr, toaddr, did_pad)) 2148 1.8 christos isym->st_size += count; 2149 1.8 christos 2150 1.8 christos if (avr_should_move_sym (symval, addr, toaddr, did_pad)) 2151 1.8 christos isym->st_value -= count; 2152 1.1 christos } 2153 1.1 christos } 2154 1.1 christos } 2155 1.1 christos 2156 1.1 christos /* Now adjust the global symbols defined in this section. */ 2157 1.8 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 2158 1.1 christos - symtab_hdr->sh_info); 2159 1.1 christos sym_hashes = elf_sym_hashes (abfd); 2160 1.1 christos end_hashes = sym_hashes + symcount; 2161 1.1 christos for (; sym_hashes < end_hashes; sym_hashes++) 2162 1.1 christos { 2163 1.1 christos struct elf_link_hash_entry *sym_hash = *sym_hashes; 2164 1.8 christos if ((sym_hash->root.type == bfd_link_hash_defined 2165 1.8 christos || sym_hash->root.type == bfd_link_hash_defweak) 2166 1.8 christos && sym_hash->root.u.def.section == sec) 2167 1.8 christos { 2168 1.8 christos symvalue symval = sym_hash->root.u.def.value; 2169 1.8 christos symvalue symend = symval + sym_hash->size; 2170 1.8 christos 2171 1.8 christos if (avr_should_reduce_sym_size (symval, symend, 2172 1.8 christos addr, toaddr, did_pad)) 2173 1.8 christos { 2174 1.8 christos /* If this assert fires then we have a symbol that ends 2175 1.8 christos part way through an instruction. Does that make 2176 1.8 christos sense? */ 2177 1.8 christos BFD_ASSERT (symend >= addr + count); 2178 1.8 christos sym_hash->size -= count; 2179 1.8 christos } 2180 1.8 christos else if (avr_should_increase_sym_size (symval, symend, 2181 1.8 christos addr, toaddr, did_pad)) 2182 1.8 christos sym_hash->size += count; 2183 1.8 christos 2184 1.8 christos if (avr_should_move_sym (symval, addr, toaddr, did_pad)) 2185 1.8 christos sym_hash->root.u.def.value -= count; 2186 1.1 christos } 2187 1.1 christos } 2188 1.10 christos 2189 1.1 christos return true; 2190 1.1 christos } 2191 1.5 christos 2192 1.5 christos static Elf_Internal_Sym * 2193 1.5 christos retrieve_local_syms (bfd *input_bfd) 2194 1.5 christos { 2195 1.5 christos Elf_Internal_Shdr *symtab_hdr; 2196 1.5 christos Elf_Internal_Sym *isymbuf; 2197 1.5 christos size_t locsymcount; 2198 1.5 christos 2199 1.5 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 2200 1.5 christos locsymcount = symtab_hdr->sh_info; 2201 1.5 christos 2202 1.5 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2203 1.5 christos if (isymbuf == NULL && locsymcount != 0) 2204 1.5 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 2205 1.5 christos NULL, NULL, NULL); 2206 1.5 christos 2207 1.5 christos /* Save the symbols for this input file so they won't be read again. */ 2208 1.5 christos if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents) 2209 1.5 christos symtab_hdr->contents = (unsigned char *) isymbuf; 2210 1.5 christos 2211 1.5 christos return isymbuf; 2212 1.5 christos } 2213 1.5 christos 2214 1.5 christos /* Get the input section for a given symbol index. 2215 1.5 christos If the symbol is: 2216 1.5 christos . a section symbol, return the section; 2217 1.5 christos . a common symbol, return the common section; 2218 1.5 christos . an undefined symbol, return the undefined section; 2219 1.5 christos . an indirect symbol, follow the links; 2220 1.5 christos . an absolute value, return the absolute section. */ 2221 1.5 christos 2222 1.5 christos static asection * 2223 1.5 christos get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx) 2224 1.5 christos { 2225 1.5 christos Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2226 1.5 christos asection *target_sec = NULL; 2227 1.5 christos if (r_symndx < symtab_hdr->sh_info) 2228 1.5 christos { 2229 1.5 christos Elf_Internal_Sym *isymbuf; 2230 1.5 christos unsigned int section_index; 2231 1.5 christos 2232 1.5 christos isymbuf = retrieve_local_syms (abfd); 2233 1.5 christos section_index = isymbuf[r_symndx].st_shndx; 2234 1.5 christos 2235 1.5 christos if (section_index == SHN_UNDEF) 2236 1.5 christos target_sec = bfd_und_section_ptr; 2237 1.5 christos else if (section_index == SHN_ABS) 2238 1.5 christos target_sec = bfd_abs_section_ptr; 2239 1.5 christos else if (section_index == SHN_COMMON) 2240 1.5 christos target_sec = bfd_com_section_ptr; 2241 1.5 christos else 2242 1.5 christos target_sec = bfd_section_from_elf_index (abfd, section_index); 2243 1.5 christos } 2244 1.5 christos else 2245 1.5 christos { 2246 1.5 christos unsigned long indx = r_symndx - symtab_hdr->sh_info; 2247 1.5 christos struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx]; 2248 1.5 christos 2249 1.8 christos while (h->root.type == bfd_link_hash_indirect 2250 1.8 christos || h->root.type == bfd_link_hash_warning) 2251 1.5 christos h = (struct elf_link_hash_entry *) h->root.u.i.link; 2252 1.5 christos 2253 1.5 christos switch (h->root.type) 2254 1.5 christos { 2255 1.5 christos case bfd_link_hash_defined: 2256 1.5 christos case bfd_link_hash_defweak: 2257 1.5 christos target_sec = h->root.u.def.section; 2258 1.5 christos break; 2259 1.5 christos case bfd_link_hash_common: 2260 1.5 christos target_sec = bfd_com_section_ptr; 2261 1.5 christos break; 2262 1.5 christos case bfd_link_hash_undefined: 2263 1.5 christos case bfd_link_hash_undefweak: 2264 1.5 christos target_sec = bfd_und_section_ptr; 2265 1.5 christos break; 2266 1.5 christos default: /* New indirect warning. */ 2267 1.5 christos target_sec = bfd_und_section_ptr; 2268 1.5 christos break; 2269 1.5 christos } 2270 1.5 christos } 2271 1.5 christos return target_sec; 2272 1.5 christos } 2273 1.5 christos 2274 1.5 christos /* Get the section-relative offset for a symbol number. */ 2275 1.5 christos 2276 1.5 christos static bfd_vma 2277 1.5 christos get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx) 2278 1.5 christos { 2279 1.5 christos Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2280 1.5 christos bfd_vma offset = 0; 2281 1.5 christos 2282 1.5 christos if (r_symndx < symtab_hdr->sh_info) 2283 1.5 christos { 2284 1.5 christos Elf_Internal_Sym *isymbuf; 2285 1.5 christos isymbuf = retrieve_local_syms (abfd); 2286 1.5 christos offset = isymbuf[r_symndx].st_value; 2287 1.5 christos } 2288 1.5 christos else 2289 1.5 christos { 2290 1.5 christos unsigned long indx = r_symndx - symtab_hdr->sh_info; 2291 1.5 christos struct elf_link_hash_entry *h = 2292 1.5 christos elf_sym_hashes (abfd)[indx]; 2293 1.5 christos 2294 1.8 christos while (h->root.type == bfd_link_hash_indirect 2295 1.5 christos || h->root.type == bfd_link_hash_warning) 2296 1.5 christos h = (struct elf_link_hash_entry *) h->root.u.i.link; 2297 1.8 christos if (h->root.type == bfd_link_hash_defined 2298 1.5 christos || h->root.type == bfd_link_hash_defweak) 2299 1.5 christos offset = h->root.u.def.value; 2300 1.5 christos } 2301 1.5 christos return offset; 2302 1.5 christos } 2303 1.5 christos 2304 1.5 christos /* Iterate over the property records in R_LIST, and copy each record into 2305 1.5 christos the list of records within the relaxation information for the section to 2306 1.5 christos which the record applies. */ 2307 1.5 christos 2308 1.5 christos static void 2309 1.5 christos avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list) 2310 1.5 christos { 2311 1.5 christos unsigned int i; 2312 1.5 christos 2313 1.5 christos for (i = 0; i < r_list->record_count; ++i) 2314 1.5 christos { 2315 1.5 christos struct avr_relax_info *relax_info; 2316 1.5 christos 2317 1.5 christos relax_info = get_avr_relax_info (r_list->records [i].section); 2318 1.5 christos BFD_ASSERT (relax_info != NULL); 2319 1.5 christos 2320 1.8 christos if (relax_info->records.count 2321 1.8 christos == relax_info->records.allocated) 2322 1.8 christos { 2323 1.8 christos /* Allocate more space. */ 2324 1.8 christos bfd_size_type size; 2325 1.8 christos 2326 1.8 christos relax_info->records.allocated += 10; 2327 1.8 christos size = (sizeof (struct avr_property_record) 2328 1.8 christos * relax_info->records.allocated); 2329 1.8 christos relax_info->records.items 2330 1.8 christos = bfd_realloc (relax_info->records.items, size); 2331 1.5 christos } 2332 1.5 christos 2333 1.8 christos memcpy (&relax_info->records.items [relax_info->records.count], 2334 1.8 christos &r_list->records [i], 2335 1.5 christos sizeof (struct avr_property_record)); 2336 1.5 christos relax_info->records.count++; 2337 1.5 christos } 2338 1.5 christos } 2339 1.5 christos 2340 1.5 christos /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the 2341 1.5 christos ordering callback from QSORT. */ 2342 1.5 christos 2343 1.5 christos static int 2344 1.5 christos avr_property_record_compare (const void *ap, const void *bp) 2345 1.5 christos { 2346 1.5 christos const struct avr_property_record *a 2347 1.5 christos = (struct avr_property_record *) ap; 2348 1.5 christos const struct avr_property_record *b 2349 1.5 christos = (struct avr_property_record *) bp; 2350 1.5 christos 2351 1.5 christos if (a->offset != b->offset) 2352 1.5 christos return (a->offset - b->offset); 2353 1.5 christos 2354 1.9 christos if (a->section != b->section) 2355 1.5 christos return bfd_section_vma (a->section) - bfd_section_vma (b->section); 2356 1.5 christos 2357 1.5 christos return (a->type - b->type); 2358 1.5 christos } 2359 1.5 christos 2360 1.5 christos /* Load all of the avr property sections from all of the bfd objects 2361 1.5 christos referenced from LINK_INFO. All of the records within each property 2362 1.5 christos section are assigned to the STRUCT AVR_RELAX_INFO within the section 2363 1.5 christos specific data of the appropriate section. */ 2364 1.5 christos 2365 1.5 christos static void 2366 1.5 christos avr_load_all_property_sections (struct bfd_link_info *link_info) 2367 1.5 christos { 2368 1.5 christos bfd *abfd; 2369 1.5 christos asection *sec; 2370 1.5 christos 2371 1.5 christos /* Initialize the per-section relaxation info. */ 2372 1.5 christos for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2373 1.5 christos for (sec = abfd->sections; sec != NULL; sec = sec->next) 2374 1.5 christos { 2375 1.5 christos init_avr_relax_info (sec); 2376 1.5 christos } 2377 1.5 christos 2378 1.5 christos /* Load the descriptor tables from .avr.prop sections. */ 2379 1.5 christos for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2380 1.5 christos { 2381 1.5 christos struct avr_property_record_list *r_list; 2382 1.5 christos 2383 1.5 christos r_list = avr_elf32_load_property_records (abfd); 2384 1.8 christos if (r_list != NULL) 2385 1.5 christos avr_elf32_assign_records_to_sections (r_list); 2386 1.5 christos 2387 1.5 christos free (r_list); 2388 1.5 christos } 2389 1.5 christos 2390 1.5 christos /* Now, for every section, ensure that the descriptor list in the 2391 1.5 christos relaxation data is sorted by ascending offset within the section. */ 2392 1.5 christos for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2393 1.5 christos for (sec = abfd->sections; sec != NULL; sec = sec->next) 2394 1.8 christos { 2395 1.8 christos struct avr_relax_info *relax_info = get_avr_relax_info (sec); 2396 1.8 christos if (relax_info && relax_info->records.count > 0) 2397 1.8 christos { 2398 1.8 christos unsigned int i; 2399 1.8 christos 2400 1.8 christos qsort (relax_info->records.items, 2401 1.8 christos relax_info->records.count, 2402 1.8 christos sizeof (struct avr_property_record), 2403 1.8 christos avr_property_record_compare); 2404 1.8 christos 2405 1.8 christos /* For debug purposes, list all the descriptors. */ 2406 1.8 christos for (i = 0; i < relax_info->records.count; ++i) 2407 1.8 christos { 2408 1.8 christos switch (relax_info->records.items [i].type) 2409 1.8 christos { 2410 1.8 christos case RECORD_ORG: 2411 1.8 christos break; 2412 1.8 christos case RECORD_ORG_AND_FILL: 2413 1.8 christos break; 2414 1.8 christos case RECORD_ALIGN: 2415 1.8 christos break; 2416 1.8 christos case RECORD_ALIGN_AND_FILL: 2417 1.8 christos break; 2418 1.8 christos }; 2419 1.8 christos } 2420 1.5 christos } 2421 1.5 christos } 2422 1.5 christos } 2423 1.1 christos 2424 1.1 christos /* This function handles relaxing for the avr. 2425 1.1 christos Many important relaxing opportunities within functions are already 2426 1.1 christos realized by the compiler itself. 2427 1.1 christos Here we try to replace call (4 bytes) -> rcall (2 bytes) 2428 1.1 christos and jump -> rjmp (safes also 2 bytes). 2429 1.1 christos As well we now optimize seqences of 2430 1.1 christos - call/rcall function 2431 1.1 christos - ret 2432 1.1 christos to yield 2433 1.1 christos - jmp/rjmp function 2434 1.1 christos - ret 2435 1.1 christos . In case that within a sequence 2436 1.1 christos - jmp/rjmp label 2437 1.1 christos - ret 2438 1.1 christos the ret could no longer be reached it is optimized away. In order 2439 1.1 christos to check if the ret is no longer needed, it is checked that the ret's address 2440 1.1 christos is not the target of a branch or jump within the same section, it is checked 2441 1.1 christos that there is no skip instruction before the jmp/rjmp and that there 2442 1.1 christos is no local or global label place at the address of the ret. 2443 1.1 christos 2444 1.1 christos We refrain from relaxing within sections ".vectors" and 2445 1.1 christos ".jumptables" in order to maintain the position of the instructions. 2446 1.1 christos There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop 2447 1.1 christos if possible. (In future one could possibly use the space of the nop 2448 1.1 christos for the first instruction of the irq service function. 2449 1.1 christos 2450 1.1 christos The .jumptables sections is meant to be used for a future tablejump variant 2451 1.1 christos for the devices with 3-byte program counter where the table itself 2452 1.1 christos contains 4-byte jump instructions whose relative offset must not 2453 1.1 christos be changed. */ 2454 1.10 christos 2455 1.1 christos static bool 2456 1.1 christos elf32_avr_relax_section (bfd *abfd, 2457 1.8 christos asection *sec, 2458 1.10 christos struct bfd_link_info *link_info, 2459 1.1 christos bool *again) 2460 1.1 christos { 2461 1.1 christos Elf_Internal_Shdr *symtab_hdr; 2462 1.1 christos Elf_Internal_Rela *internal_relocs; 2463 1.1 christos Elf_Internal_Rela *irel, *irelend; 2464 1.1 christos bfd_byte *contents = NULL; 2465 1.1 christos Elf_Internal_Sym *isymbuf = NULL; 2466 1.10 christos struct elf32_avr_link_hash_table *htab; 2467 1.5 christos static bool relaxation_initialised = false; 2468 1.5 christos 2469 1.5 christos if (!relaxation_initialised) 2470 1.10 christos { 2471 1.5 christos relaxation_initialised = true; 2472 1.5 christos 2473 1.5 christos /* Load entries from the .avr.prop sections. */ 2474 1.5 christos avr_load_all_property_sections (link_info); 2475 1.1 christos } 2476 1.1 christos 2477 1.1 christos /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while 2478 1.1 christos relaxing. Such shrinking can cause issues for the sections such 2479 1.1 christos as .vectors and .jumptables. Instead the unused bytes should be 2480 1.10 christos filled with nop instructions. */ 2481 1.1 christos bool shrinkable = true; 2482 1.1 christos 2483 1.1 christos if (!strcmp (sec->name,".vectors") 2484 1.10 christos || !strcmp (sec->name,".jumptables")) 2485 1.1 christos shrinkable = false; 2486 1.6 christos 2487 1.1 christos if (bfd_link_relocatable (link_info)) 2488 1.1 christos (*link_info->callbacks->einfo) 2489 1.1 christos (_("%P%F: --relax and -r may not be used together\n")); 2490 1.1 christos 2491 1.1 christos htab = avr_link_hash_table (link_info); 2492 1.10 christos if (htab == NULL) 2493 1.1 christos return false; 2494 1.1 christos 2495 1.10 christos /* Assume nothing changes. */ 2496 1.1 christos *again = false; 2497 1.1 christos 2498 1.1 christos if ((!htab->no_stubs) && (sec == htab->stub_sec)) 2499 1.1 christos { 2500 1.1 christos /* We are just relaxing the stub section. 2501 1.1 christos Let's calculate the size needed again. */ 2502 1.1 christos bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size; 2503 1.1 christos 2504 1.8 christos if (debug_relax) 2505 1.8 christos printf ("Relaxing the stub section. Size prior to this pass: %i\n", 2506 1.1 christos (int) last_estimated_stub_section_size); 2507 1.1 christos 2508 1.10 christos elf32_avr_size_stubs (htab->stub_sec->output_section->owner, 2509 1.1 christos link_info, false); 2510 1.1 christos 2511 1.1 christos /* Check if the number of trampolines changed. */ 2512 1.10 christos if (last_estimated_stub_section_size != htab->stub_sec->size) 2513 1.1 christos *again = true; 2514 1.1 christos 2515 1.8 christos if (debug_relax) 2516 1.8 christos printf ("Size of stub section after this pass: %i\n", 2517 1.1 christos (int) htab->stub_sec->size); 2518 1.10 christos 2519 1.1 christos return true; 2520 1.1 christos } 2521 1.1 christos 2522 1.1 christos /* We don't have to do anything for a relocatable link, if 2523 1.1 christos this section does not have relocs, or if this is not a 2524 1.6 christos code section. */ 2525 1.11 christos if (bfd_link_relocatable (link_info) 2526 1.1 christos || sec->reloc_count == 0 2527 1.11 christos || (sec->flags & SEC_RELOC) == 0 2528 1.1 christos || (sec->flags & SEC_HAS_CONTENTS) == 0 2529 1.10 christos || (sec->flags & SEC_CODE) == 0) 2530 1.1 christos return true; 2531 1.1 christos 2532 1.1 christos /* Check if the object file to relax uses internal symbols so that we 2533 1.1 christos could fix up the relocations. */ 2534 1.10 christos if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED)) 2535 1.1 christos return true; 2536 1.1 christos 2537 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2538 1.1 christos 2539 1.1 christos /* Get a copy of the native relocations. */ 2540 1.8 christos internal_relocs = (_bfd_elf_link_read_relocs 2541 1.1 christos (abfd, sec, NULL, NULL, link_info->keep_memory)); 2542 1.1 christos if (internal_relocs == NULL) 2543 1.1 christos goto error_return; 2544 1.1 christos 2545 1.1 christos /* Walk through the relocs looking for relaxing opportunities. */ 2546 1.1 christos irelend = internal_relocs + sec->reloc_count; 2547 1.1 christos for (irel = internal_relocs; irel < irelend; irel++) 2548 1.1 christos { 2549 1.1 christos bfd_vma symval; 2550 1.1 christos 2551 1.3 christos if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL 2552 1.3 christos && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL 2553 1.8 christos && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL) 2554 1.1 christos continue; 2555 1.1 christos 2556 1.1 christos /* Get the section contents if we haven't done so already. */ 2557 1.8 christos if (contents == NULL) 2558 1.8 christos { 2559 1.8 christos /* Get cached copy if it exists. */ 2560 1.8 christos if (elf_section_data (sec)->this_hdr.contents != NULL) 2561 1.8 christos contents = elf_section_data (sec)->this_hdr.contents; 2562 1.8 christos else 2563 1.8 christos { 2564 1.8 christos /* Go get them off disk. */ 2565 1.8 christos if (! bfd_malloc_and_get_section (abfd, sec, &contents)) 2566 1.8 christos goto error_return; 2567 1.8 christos } 2568 1.1 christos } 2569 1.1 christos 2570 1.1 christos /* Read this BFD's local symbols if we haven't done so already. */ 2571 1.8 christos if (isymbuf == NULL && symtab_hdr->sh_info != 0) 2572 1.8 christos { 2573 1.8 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2574 1.8 christos if (isymbuf == NULL) 2575 1.8 christos isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 2576 1.8 christos symtab_hdr->sh_info, 0, 2577 1.8 christos NULL, NULL, NULL); 2578 1.8 christos if (isymbuf == NULL) 2579 1.8 christos goto error_return; 2580 1.1 christos } 2581 1.1 christos 2582 1.1 christos 2583 1.1 christos /* Get the value of the symbol referred to by the reloc. */ 2584 1.8 christos if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 2585 1.8 christos { 2586 1.8 christos /* A local symbol. */ 2587 1.8 christos Elf_Internal_Sym *isym; 2588 1.8 christos asection *sym_sec; 2589 1.8 christos 2590 1.8 christos isym = isymbuf + ELF32_R_SYM (irel->r_info); 2591 1.8 christos sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 2592 1.8 christos symval = isym->st_value; 2593 1.8 christos /* If the reloc is absolute, it will not have 2594 1.8 christos a symbol or section associated with it. */ 2595 1.8 christos if (sym_sec) 2596 1.8 christos symval += sym_sec->output_section->vma 2597 1.8 christos + sym_sec->output_offset; 2598 1.1 christos } 2599 1.8 christos else 2600 1.8 christos { 2601 1.8 christos unsigned long indx; 2602 1.8 christos struct elf_link_hash_entry *h; 2603 1.8 christos 2604 1.8 christos /* An external symbol. */ 2605 1.8 christos indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; 2606 1.8 christos h = elf_sym_hashes (abfd)[indx]; 2607 1.8 christos BFD_ASSERT (h != NULL); 2608 1.8 christos if (h->root.type != bfd_link_hash_defined 2609 1.1 christos && h->root.type != bfd_link_hash_defweak) 2610 1.1 christos /* This appears to be a reference to an undefined 2611 1.1 christos symbol. Just ignore it--it will be caught by the 2612 1.1 christos regular reloc processing. */ 2613 1.1 christos continue; 2614 1.8 christos 2615 1.8 christos symval = (h->root.u.def.value 2616 1.8 christos + h->root.u.def.section->output_section->vma 2617 1.8 christos + h->root.u.def.section->output_offset); 2618 1.1 christos } 2619 1.1 christos 2620 1.8 christos /* For simplicity of coding, we are going to modify the section 2621 1.8 christos contents, the section relocs, and the BFD symbol table. We 2622 1.8 christos must tell the rest of the code not to free up this 2623 1.8 christos information. It would be possible to instead create a table 2624 1.8 christos of changes which have to be made, as is done in coff-mips.c; 2625 1.8 christos that would be more work, but would require less memory when 2626 1.1 christos the linker is run. */ 2627 1.8 christos switch (ELF32_R_TYPE (irel->r_info)) 2628 1.1 christos { 2629 1.1 christos /* Try to turn a 22-bit absolute call/jump into an 13-bit 2630 1.1 christos pc-relative rcall/rjmp. */ 2631 1.8 christos case R_AVR_CALL: 2632 1.8 christos { 2633 1.8 christos bfd_vma value = symval + irel->r_addend; 2634 1.8 christos bfd_vma dot, gap; 2635 1.8 christos int distance_short_enough = 0; 2636 1.8 christos 2637 1.8 christos /* Get the address of this instruction. */ 2638 1.8 christos dot = (sec->output_section->vma 2639 1.8 christos + sec->output_offset + irel->r_offset); 2640 1.8 christos 2641 1.8 christos /* Compute the distance from this insn to the branch target. */ 2642 1.8 christos gap = value - dot; 2643 1.9 christos 2644 1.9 christos /* The ISA manual states that addressable range is PC - 2k + 1 to 2645 1.9 christos PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range 2646 1.9 christos is shifted one word to the right, because pc-relative instructions 2647 1.9 christos implicitly add one word i.e. rjmp 0 jumps to next insn, not the 2648 1.9 christos current one. 2649 1.9 christos Therefore, for the !shrinkable case, the range is as above. 2650 1.9 christos If shrinkable, then the current code only deletes bytes 3 and 2651 1.9 christos 4 of the absolute call/jmp, so the forward jump range increases 2652 1.9 christos by 2 bytes, but the backward (negative) jump range remains 2653 1.9 christos the same. */ 2654 1.9 christos 2655 1.8 christos 2656 1.8 christos /* Check if the gap falls in the range that can be accommodated 2657 1.8 christos in 13bits signed (It is 12bits when encoded, as we deal with 2658 1.9 christos word addressing). */ 2659 1.8 christos if (!shrinkable && ((int) gap >= -4094 && (int) gap <= 4096)) 2660 1.8 christos distance_short_enough = 1; 2661 1.9 christos /* If shrinkable, then we can check for a range of distance which 2662 1.8 christos is two bytes farther on the positive direction because the call 2663 1.8 christos or jump target will be closer by two bytes after the 2664 1.9 christos relaxation. */ 2665 1.8 christos else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4098)) 2666 1.8 christos distance_short_enough = 1; 2667 1.8 christos 2668 1.8 christos /* Here we handle the wrap-around case. E.g. for a 16k device 2669 1.8 christos we could use a rjmp to jump from address 0x100 to 0x3d00! 2670 1.8 christos In order to make this work properly, we need to fill the 2671 1.8 christos vaiable avr_pc_wrap_around with the appropriate value. 2672 1.8 christos I.e. 0x4000 for a 16k device. */ 2673 1.1 christos { 2674 1.1 christos /* Shrinking the code size makes the gaps larger in the 2675 1.1 christos case of wrap-arounds. So we use a heuristical safety 2676 1.1 christos margin to avoid that during relax the distance gets 2677 1.1 christos again too large for the short jumps. Let's assume 2678 1.1 christos a typical code-size reduction due to relax for a 2679 1.1 christos 16k device of 600 bytes. So let's use twice the 2680 1.1 christos typical value as safety margin. */ 2681 1.1 christos int rgap; 2682 1.1 christos int safety_margin; 2683 1.1 christos 2684 1.1 christos int assumed_shrink = 600; 2685 1.1 christos if (avr_pc_wrap_around > 0x4000) 2686 1.1 christos assumed_shrink = 900; 2687 1.1 christos 2688 1.1 christos safety_margin = 2 * assumed_shrink; 2689 1.1 christos 2690 1.1 christos rgap = avr_relative_distance_considering_wrap_around (gap); 2691 1.1 christos 2692 1.1 christos if (rgap >= (-4092 + safety_margin) 2693 1.1 christos && rgap <= (4094 - safety_margin)) 2694 1.8 christos distance_short_enough = 1; 2695 1.1 christos } 2696 1.8 christos 2697 1.8 christos if (distance_short_enough) 2698 1.8 christos { 2699 1.8 christos unsigned char code_msb; 2700 1.8 christos unsigned char code_lsb; 2701 1.8 christos 2702 1.8 christos if (debug_relax) 2703 1.8 christos printf ("shrinking jump/call instruction at address 0x%x" 2704 1.8 christos " in section %s\n\n", 2705 1.8 christos (int) dot, sec->name); 2706 1.8 christos 2707 1.8 christos /* Note that we've changed the relocs, section contents, 2708 1.8 christos etc. */ 2709 1.8 christos elf_section_data (sec)->relocs = internal_relocs; 2710 1.8 christos elf_section_data (sec)->this_hdr.contents = contents; 2711 1.8 christos symtab_hdr->contents = (unsigned char *) isymbuf; 2712 1.8 christos 2713 1.8 christos /* Get the instruction code for relaxing. */ 2714 1.8 christos code_lsb = bfd_get_8 (abfd, contents + irel->r_offset); 2715 1.8 christos code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); 2716 1.8 christos 2717 1.8 christos /* Mask out the relocation bits. */ 2718 1.8 christos code_msb &= 0x94; 2719 1.8 christos code_lsb &= 0x0E; 2720 1.8 christos if (code_msb == 0x94 && code_lsb == 0x0E) 2721 1.8 christos { 2722 1.8 christos /* we are changing call -> rcall . */ 2723 1.8 christos bfd_put_8 (abfd, 0x00, contents + irel->r_offset); 2724 1.8 christos bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1); 2725 1.8 christos } 2726 1.8 christos else if (code_msb == 0x94 && code_lsb == 0x0C) 2727 1.8 christos { 2728 1.8 christos /* we are changeing jump -> rjmp. */ 2729 1.8 christos bfd_put_8 (abfd, 0x00, contents + irel->r_offset); 2730 1.8 christos bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1); 2731 1.8 christos } 2732 1.8 christos else 2733 1.8 christos abort (); 2734 1.8 christos 2735 1.8 christos /* Fix the relocation's type. */ 2736 1.8 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 2737 1.8 christos R_AVR_13_PCREL); 2738 1.8 christos 2739 1.8 christos /* We should not modify the ordering if 'shrinkable' is 2740 1.8 christos FALSE. */ 2741 1.8 christos if (!shrinkable) 2742 1.8 christos { 2743 1.8 christos /* Let's insert a nop. */ 2744 1.8 christos bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2); 2745 1.8 christos bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3); 2746 1.8 christos } 2747 1.8 christos else 2748 1.8 christos { 2749 1.8 christos /* Delete two bytes of data. */ 2750 1.8 christos if (!elf32_avr_relax_delete_bytes (abfd, sec, 2751 1.10 christos irel->r_offset + 2, 2, 2752 1.8 christos true)) 2753 1.8 christos goto error_return; 2754 1.8 christos 2755 1.8 christos /* That will change things, so, we should relax again. 2756 1.10 christos Note that this is not required, and it may be slow. */ 2757 1.8 christos *again = true; 2758 1.8 christos } 2759 1.8 christos } 2760 1.7 christos } 2761 1.1 christos /* Fall through. */ 2762 1.8 christos 2763 1.8 christos default: 2764 1.8 christos { 2765 1.8 christos unsigned char code_msb; 2766 1.8 christos unsigned char code_lsb; 2767 1.8 christos bfd_vma dot; 2768 1.8 christos 2769 1.8 christos code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); 2770 1.8 christos code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0); 2771 1.8 christos 2772 1.8 christos /* Get the address of this instruction. */ 2773 1.8 christos dot = (sec->output_section->vma 2774 1.8 christos + sec->output_offset + irel->r_offset); 2775 1.8 christos 2776 1.8 christos /* Here we look for rcall/ret or call/ret sequences that could be 2777 1.8 christos safely replaced by rjmp/ret or jmp/ret. */ 2778 1.8 christos if (((code_msb & 0xf0) == 0xd0) 2779 1.8 christos && avr_replace_call_ret_sequences) 2780 1.8 christos { 2781 1.8 christos /* This insn is a rcall. */ 2782 1.8 christos unsigned char next_insn_msb = 0; 2783 1.8 christos unsigned char next_insn_lsb = 0; 2784 1.8 christos 2785 1.8 christos if (irel->r_offset + 3 < sec->size) 2786 1.8 christos { 2787 1.1 christos next_insn_msb = 2788 1.8 christos bfd_get_8 (abfd, contents + irel->r_offset + 3); 2789 1.1 christos next_insn_lsb = 2790 1.8 christos bfd_get_8 (abfd, contents + irel->r_offset + 2); 2791 1.1 christos } 2792 1.1 christos 2793 1.8 christos if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2794 1.8 christos { 2795 1.8 christos /* The next insn is a ret. We now convert the rcall insn 2796 1.8 christos into a rjmp instruction. */ 2797 1.8 christos code_msb &= 0xef; 2798 1.8 christos bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1); 2799 1.8 christos if (debug_relax) 2800 1.8 christos printf ("converted rcall/ret sequence at address 0x%x" 2801 1.8 christos " into rjmp/ret sequence. Section is %s\n\n", 2802 1.10 christos (int) dot, sec->name); 2803 1.8 christos *again = true; 2804 1.8 christos break; 2805 1.8 christos } 2806 1.8 christos } 2807 1.1 christos else if ((0x94 == (code_msb & 0xfe)) 2808 1.1 christos && (0x0e == (code_lsb & 0x0e)) 2809 1.8 christos && avr_replace_call_ret_sequences) 2810 1.8 christos { 2811 1.8 christos /* This insn is a call. */ 2812 1.8 christos unsigned char next_insn_msb = 0; 2813 1.8 christos unsigned char next_insn_lsb = 0; 2814 1.8 christos 2815 1.8 christos if (irel->r_offset + 5 < sec->size) 2816 1.8 christos { 2817 1.1 christos next_insn_msb = 2818 1.8 christos bfd_get_8 (abfd, contents + irel->r_offset + 5); 2819 1.1 christos next_insn_lsb = 2820 1.8 christos bfd_get_8 (abfd, contents + irel->r_offset + 4); 2821 1.1 christos } 2822 1.8 christos 2823 1.8 christos if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2824 1.8 christos { 2825 1.8 christos /* The next insn is a ret. We now convert the call insn 2826 1.8 christos into a jmp instruction. */ 2827 1.8 christos 2828 1.8 christos code_lsb &= 0xfd; 2829 1.8 christos bfd_put_8 (abfd, code_lsb, contents + irel->r_offset); 2830 1.8 christos if (debug_relax) 2831 1.8 christos printf ("converted call/ret sequence at address 0x%x" 2832 1.8 christos " into jmp/ret sequence. Section is %s\n\n", 2833 1.10 christos (int) dot, sec->name); 2834 1.8 christos *again = true; 2835 1.8 christos break; 2836 1.8 christos } 2837 1.8 christos } 2838 1.8 christos else if ((0xc0 == (code_msb & 0xf0)) 2839 1.8 christos || ((0x94 == (code_msb & 0xfe)) 2840 1.8 christos && (0x0c == (code_lsb & 0x0e)))) 2841 1.8 christos { 2842 1.8 christos /* This insn is a rjmp or a jmp. */ 2843 1.8 christos unsigned char next_insn_msb = 0; 2844 1.8 christos unsigned char next_insn_lsb = 0; 2845 1.8 christos int insn_size; 2846 1.8 christos 2847 1.8 christos if (0xc0 == (code_msb & 0xf0)) 2848 1.8 christos insn_size = 2; /* rjmp insn */ 2849 1.8 christos else 2850 1.8 christos insn_size = 4; /* jmp insn */ 2851 1.8 christos 2852 1.8 christos if (irel->r_offset + insn_size + 1 < sec->size) 2853 1.8 christos { 2854 1.1 christos next_insn_msb = 2855 1.1 christos bfd_get_8 (abfd, contents + irel->r_offset 2856 1.8 christos + insn_size + 1); 2857 1.1 christos next_insn_lsb = 2858 1.1 christos bfd_get_8 (abfd, contents + irel->r_offset 2859 1.8 christos + insn_size); 2860 1.1 christos } 2861 1.8 christos 2862 1.8 christos if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2863 1.8 christos { 2864 1.8 christos /* The next insn is a ret. We possibly could delete 2865 1.8 christos this ret. First we need to check for preceding 2866 1.8 christos sbis/sbic/sbrs or cpse "skip" instructions. */ 2867 1.8 christos 2868 1.8 christos int there_is_preceding_non_skip_insn = 1; 2869 1.8 christos bfd_vma address_of_ret; 2870 1.8 christos 2871 1.8 christos address_of_ret = dot + insn_size; 2872 1.8 christos 2873 1.8 christos if (debug_relax && (insn_size == 2)) 2874 1.8 christos printf ("found rjmp / ret sequence at address 0x%x\n", 2875 1.8 christos (int) dot); 2876 1.8 christos if (debug_relax && (insn_size == 4)) 2877 1.8 christos printf ("found jmp / ret sequence at address 0x%x\n", 2878 1.8 christos (int) dot); 2879 1.8 christos 2880 1.8 christos /* We have to make sure that there is a preceding insn. */ 2881 1.8 christos if (irel->r_offset >= 2) 2882 1.8 christos { 2883 1.8 christos unsigned char preceding_msb; 2884 1.1 christos unsigned char preceding_lsb; 2885 1.8 christos 2886 1.1 christos preceding_msb = 2887 1.8 christos bfd_get_8 (abfd, contents + irel->r_offset - 1); 2888 1.1 christos preceding_lsb = 2889 1.1 christos bfd_get_8 (abfd, contents + irel->r_offset - 2); 2890 1.8 christos 2891 1.8 christos /* sbic. */ 2892 1.8 christos if (0x99 == preceding_msb) 2893 1.8 christos there_is_preceding_non_skip_insn = 0; 2894 1.8 christos 2895 1.8 christos /* sbis. */ 2896 1.8 christos if (0x9b == preceding_msb) 2897 1.1 christos there_is_preceding_non_skip_insn = 0; 2898 1.8 christos 2899 1.8 christos /* sbrc */ 2900 1.1 christos if ((0xfc == (preceding_msb & 0xfe) 2901 1.8 christos && (0x00 == (preceding_lsb & 0x08)))) 2902 1.1 christos there_is_preceding_non_skip_insn = 0; 2903 1.8 christos 2904 1.8 christos /* sbrs */ 2905 1.1 christos if ((0xfe == (preceding_msb & 0xfe) 2906 1.8 christos && (0x00 == (preceding_lsb & 0x08)))) 2907 1.1 christos there_is_preceding_non_skip_insn = 0; 2908 1.8 christos 2909 1.8 christos /* cpse */ 2910 1.8 christos if (0x10 == (preceding_msb & 0xfc)) 2911 1.8 christos there_is_preceding_non_skip_insn = 0; 2912 1.8 christos 2913 1.8 christos if (there_is_preceding_non_skip_insn == 0) 2914 1.8 christos if (debug_relax) 2915 1.8 christos printf ("preceding skip insn prevents deletion of" 2916 1.8 christos " ret insn at Addy 0x%x in section %s\n", 2917 1.8 christos (int) dot + 2, sec->name); 2918 1.8 christos } 2919 1.8 christos else 2920 1.8 christos { 2921 1.8 christos /* There is no previous instruction. */ 2922 1.8 christos there_is_preceding_non_skip_insn = 0; 2923 1.8 christos } 2924 1.8 christos 2925 1.8 christos if (there_is_preceding_non_skip_insn) 2926 1.8 christos { 2927 1.8 christos /* We now only have to make sure that there is no 2928 1.8 christos local label defined at the address of the ret 2929 1.8 christos instruction and that there is no local relocation 2930 1.1 christos in this section pointing to the ret. */ 2931 1.8 christos 2932 1.8 christos int deleting_ret_is_safe = 1; 2933 1.1 christos unsigned int section_offset_of_ret_insn = 2934 1.8 christos irel->r_offset + insn_size; 2935 1.8 christos Elf_Internal_Sym *isym, *isymend; 2936 1.1 christos unsigned int sec_shndx; 2937 1.1 christos struct bfd_section *isec; 2938 1.8 christos 2939 1.1 christos sec_shndx = 2940 1.1 christos _bfd_elf_section_from_bfd_section (abfd, sec); 2941 1.8 christos 2942 1.8 christos /* Check for local symbols. */ 2943 1.8 christos isym = (Elf_Internal_Sym *) symtab_hdr->contents; 2944 1.1 christos isymend = isym + symtab_hdr->sh_info; 2945 1.8 christos /* PR 6019: There may not be any local symbols. */ 2946 1.1 christos for (; isym != NULL && isym < isymend; isym++) 2947 1.1 christos { 2948 1.1 christos if (isym->st_value == section_offset_of_ret_insn 2949 1.1 christos && isym->st_shndx == sec_shndx) 2950 1.1 christos { 2951 1.1 christos deleting_ret_is_safe = 0; 2952 1.1 christos if (debug_relax) 2953 1.1 christos printf ("local label prevents deletion of ret " 2954 1.1 christos "insn at address 0x%x\n", 2955 1.1 christos (int) dot + insn_size); 2956 1.1 christos } 2957 1.1 christos } 2958 1.1 christos 2959 1.1 christos /* Now check for global symbols. */ 2960 1.1 christos { 2961 1.1 christos int symcount; 2962 1.1 christos struct elf_link_hash_entry **sym_hashes; 2963 1.1 christos struct elf_link_hash_entry **end_hashes; 2964 1.1 christos 2965 1.1 christos symcount = (symtab_hdr->sh_size 2966 1.1 christos / sizeof (Elf32_External_Sym) 2967 1.1 christos - symtab_hdr->sh_info); 2968 1.1 christos sym_hashes = elf_sym_hashes (abfd); 2969 1.1 christos end_hashes = sym_hashes + symcount; 2970 1.1 christos for (; sym_hashes < end_hashes; sym_hashes++) 2971 1.1 christos { 2972 1.1 christos struct elf_link_hash_entry *sym_hash = 2973 1.1 christos *sym_hashes; 2974 1.1 christos if ((sym_hash->root.type == bfd_link_hash_defined 2975 1.1 christos || sym_hash->root.type == 2976 1.1 christos bfd_link_hash_defweak) 2977 1.1 christos && sym_hash->root.u.def.section == sec 2978 1.1 christos && sym_hash->root.u.def.value == section_offset_of_ret_insn) 2979 1.1 christos { 2980 1.1 christos deleting_ret_is_safe = 0; 2981 1.1 christos if (debug_relax) 2982 1.1 christos printf ("global label prevents deletion of " 2983 1.1 christos "ret insn at address 0x%x\n", 2984 1.1 christos (int) dot + insn_size); 2985 1.1 christos } 2986 1.1 christos } 2987 1.1 christos } 2988 1.1 christos 2989 1.1 christos /* Now we check for relocations pointing to ret. */ 2990 1.1 christos for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next) 2991 1.1 christos { 2992 1.1 christos Elf_Internal_Rela *rel; 2993 1.3 christos Elf_Internal_Rela *relend; 2994 1.1 christos 2995 1.1 christos rel = elf_section_data (isec)->relocs; 2996 1.10 christos if (rel == NULL) 2997 1.1 christos rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true); 2998 1.1 christos 2999 1.1 christos relend = rel + isec->reloc_count; 3000 1.1 christos 3001 1.1 christos for (; rel && rel < relend; rel++) 3002 1.1 christos { 3003 1.1 christos bfd_vma reloc_target = 0; 3004 1.1 christos 3005 1.1 christos /* Read this BFD's local symbols if we haven't 3006 1.1 christos done so already. */ 3007 1.1 christos if (isymbuf == NULL && symtab_hdr->sh_info != 0) 3008 1.1 christos { 3009 1.1 christos isymbuf = (Elf_Internal_Sym *) 3010 1.1 christos symtab_hdr->contents; 3011 1.1 christos if (isymbuf == NULL) 3012 1.1 christos isymbuf = bfd_elf_get_elf_syms 3013 1.1 christos (abfd, 3014 1.1 christos symtab_hdr, 3015 1.1 christos symtab_hdr->sh_info, 0, 3016 1.1 christos NULL, NULL, NULL); 3017 1.1 christos if (isymbuf == NULL) 3018 1.1 christos break; 3019 1.1 christos } 3020 1.1 christos 3021 1.1 christos /* Get the value of the symbol referred to 3022 1.1 christos by the reloc. */ 3023 1.1 christos if (ELF32_R_SYM (rel->r_info) 3024 1.1 christos < symtab_hdr->sh_info) 3025 1.1 christos { 3026 1.1 christos /* A local symbol. */ 3027 1.1 christos asection *sym_sec; 3028 1.1 christos 3029 1.1 christos isym = isymbuf 3030 1.1 christos + ELF32_R_SYM (rel->r_info); 3031 1.1 christos sym_sec = bfd_section_from_elf_index 3032 1.1 christos (abfd, isym->st_shndx); 3033 1.1 christos symval = isym->st_value; 3034 1.1 christos 3035 1.1 christos /* If the reloc is absolute, it will not 3036 1.1 christos have a symbol or section associated 3037 1.1 christos with it. */ 3038 1.1 christos 3039 1.1 christos if (sym_sec) 3040 1.1 christos { 3041 1.1 christos symval += 3042 1.1 christos sym_sec->output_section->vma 3043 1.1 christos + sym_sec->output_offset; 3044 1.1 christos reloc_target = symval + rel->r_addend; 3045 1.1 christos } 3046 1.1 christos else 3047 1.1 christos { 3048 1.1 christos reloc_target = symval + rel->r_addend; 3049 1.1 christos /* Reference symbol is absolute. */ 3050 1.1 christos } 3051 1.1 christos } 3052 1.1 christos /* else ... reference symbol is extern. */ 3053 1.1 christos 3054 1.1 christos if (address_of_ret == reloc_target) 3055 1.1 christos { 3056 1.1 christos deleting_ret_is_safe = 0; 3057 1.1 christos if (debug_relax) 3058 1.1 christos printf ("ret from " 3059 1.1 christos "rjmp/jmp ret sequence at address" 3060 1.1 christos " 0x%x could not be deleted. ret" 3061 1.1 christos " is target of a relocation.\n", 3062 1.1 christos (int) address_of_ret); 3063 1.1 christos break; 3064 1.1 christos } 3065 1.1 christos } 3066 1.1 christos } 3067 1.1 christos 3068 1.1 christos if (deleting_ret_is_safe) 3069 1.1 christos { 3070 1.1 christos if (debug_relax) 3071 1.1 christos printf ("unreachable ret instruction " 3072 1.1 christos "at address 0x%x deleted.\n", 3073 1.1 christos (int) dot + insn_size); 3074 1.10 christos 3075 1.10 christos elf_section_data (sec)->relocs = internal_relocs; 3076 1.10 christos elf_section_data (sec)->this_hdr.contents = contents; 3077 1.10 christos symtab_hdr->contents = (unsigned char *) isymbuf; 3078 1.1 christos 3079 1.1 christos /* Delete two bytes of data. */ 3080 1.7 christos if (!elf32_avr_relax_delete_bytes (abfd, sec, 3081 1.10 christos irel->r_offset + insn_size, 2, 3082 1.1 christos true)) 3083 1.1 christos goto error_return; 3084 1.1 christos 3085 1.1 christos /* That will change things, so, we should relax 3086 1.1 christos again. Note that this is not required, and it 3087 1.10 christos may be slow. */ 3088 1.1 christos *again = true; 3089 1.1 christos break; 3090 1.8 christos } 3091 1.8 christos } 3092 1.8 christos } 3093 1.8 christos } 3094 1.8 christos break; 3095 1.8 christos } 3096 1.1 christos } 3097 1.1 christos } 3098 1.5 christos 3099 1.5 christos if (!*again) 3100 1.5 christos { 3101 1.8 christos /* Look through all the property records in this section to see if 3102 1.5 christos there's any alignment records that can be moved. */ 3103 1.5 christos struct avr_relax_info *relax_info; 3104 1.5 christos 3105 1.5 christos relax_info = get_avr_relax_info (sec); 3106 1.8 christos if (relax_info->records.count > 0) 3107 1.8 christos { 3108 1.5 christos unsigned int i; 3109 1.8 christos 3110 1.8 christos for (i = 0; i < relax_info->records.count; ++i) 3111 1.8 christos { 3112 1.8 christos switch (relax_info->records.items [i].type) 3113 1.8 christos { 3114 1.8 christos case RECORD_ORG: 3115 1.8 christos case RECORD_ORG_AND_FILL: 3116 1.8 christos break; 3117 1.8 christos case RECORD_ALIGN: 3118 1.8 christos case RECORD_ALIGN_AND_FILL: 3119 1.8 christos { 3120 1.8 christos struct avr_property_record *record; 3121 1.8 christos unsigned long bytes_to_align; 3122 1.8 christos int count = 0; 3123 1.8 christos 3124 1.8 christos /* Look for alignment directives that have had enough 3125 1.8 christos bytes deleted before them, such that the directive 3126 1.8 christos can be moved backwards and still maintain the 3127 1.8 christos required alignment. */ 3128 1.8 christos record = &relax_info->records.items [i]; 3129 1.8 christos bytes_to_align 3130 1.8 christos = (unsigned long) (1 << record->data.align.bytes); 3131 1.8 christos while (record->data.align.preceding_deleted >= 3132 1.8 christos bytes_to_align) 3133 1.8 christos { 3134 1.8 christos record->data.align.preceding_deleted 3135 1.8 christos -= bytes_to_align; 3136 1.8 christos count += bytes_to_align; 3137 1.8 christos } 3138 1.8 christos 3139 1.8 christos if (count > 0) 3140 1.8 christos { 3141 1.8 christos bfd_vma addr = record->offset; 3142 1.8 christos 3143 1.8 christos /* We can delete COUNT bytes and this alignment 3144 1.8 christos directive will still be correctly aligned. 3145 1.8 christos First move the alignment directive, then delete 3146 1.8 christos the bytes. */ 3147 1.8 christos record->offset -= count; 3148 1.8 christos elf32_avr_relax_delete_bytes (abfd, sec, 3149 1.10 christos addr - count, 3150 1.10 christos count, false); 3151 1.8 christos *again = true; 3152 1.8 christos } 3153 1.8 christos } 3154 1.8 christos break; 3155 1.8 christos } 3156 1.8 christos } 3157 1.5 christos } 3158 1.5 christos } 3159 1.1 christos 3160 1.1 christos if (contents != NULL 3161 1.1 christos && elf_section_data (sec)->this_hdr.contents != contents) 3162 1.1 christos { 3163 1.8 christos if (! link_info->keep_memory) 3164 1.1 christos free (contents); 3165 1.8 christos else 3166 1.8 christos { 3167 1.8 christos /* Cache the section contents for elf_link_input_bfd. */ 3168 1.8 christos elf_section_data (sec)->this_hdr.contents = contents; 3169 1.1 christos } 3170 1.1 christos } 3171 1.9 christos 3172 1.1 christos if (elf_section_data (sec)->relocs != internal_relocs) 3173 1.1 christos free (internal_relocs); 3174 1.10 christos 3175 1.1 christos return true; 3176 1.1 christos 3177 1.9 christos error_return: 3178 1.1 christos if (symtab_hdr->contents != (unsigned char *) isymbuf) 3179 1.9 christos free (isymbuf); 3180 1.1 christos if (elf_section_data (sec)->this_hdr.contents != contents) 3181 1.9 christos free (contents); 3182 1.1 christos if (elf_section_data (sec)->relocs != internal_relocs) 3183 1.1 christos free (internal_relocs); 3184 1.10 christos 3185 1.1 christos return false; 3186 1.1 christos } 3187 1.1 christos 3188 1.1 christos /* This is a version of bfd_generic_get_relocated_section_contents 3189 1.1 christos which uses elf32_avr_relocate_section. 3190 1.1 christos 3191 1.1 christos For avr it's essentially a cut and paste taken from the H8300 port. 3192 1.1 christos The author of the relaxation support patch for avr had absolutely no 3193 1.1 christos clue what is happening here but found out that this part of the code 3194 1.1 christos seems to be important. */ 3195 1.1 christos 3196 1.1 christos static bfd_byte * 3197 1.8 christos elf32_avr_get_relocated_section_contents (bfd *output_bfd, 3198 1.8 christos struct bfd_link_info *link_info, 3199 1.8 christos struct bfd_link_order *link_order, 3200 1.10 christos bfd_byte *data, 3201 1.8 christos bool relocatable, 3202 1.1 christos asymbol **symbols) 3203 1.1 christos { 3204 1.1 christos Elf_Internal_Shdr *symtab_hdr; 3205 1.1 christos asection *input_section = link_order->u.indirect.section; 3206 1.1 christos bfd *input_bfd = input_section->owner; 3207 1.1 christos asection **sections = NULL; 3208 1.1 christos Elf_Internal_Rela *internal_relocs = NULL; 3209 1.1 christos Elf_Internal_Sym *isymbuf = NULL; 3210 1.1 christos 3211 1.1 christos /* We only need to handle the case of relaxing, or of having a 3212 1.1 christos particular set of section contents, specially. */ 3213 1.1 christos if (relocatable 3214 1.1 christos || elf_section_data (input_section)->this_hdr.contents == NULL) 3215 1.8 christos return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 3216 1.8 christos link_order, data, 3217 1.8 christos relocatable, 3218 1.1 christos symbols); 3219 1.1 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3220 1.10 christos 3221 1.10 christos bfd_byte *orig_data = data; 3222 1.10 christos if (data == NULL) 3223 1.10 christos { 3224 1.10 christos data = bfd_malloc (input_section->size); 3225 1.10 christos if (data == NULL) 3226 1.10 christos return NULL; 3227 1.1 christos } 3228 1.8 christos memcpy (data, elf_section_data (input_section)->this_hdr.contents, 3229 1.1 christos (size_t) input_section->size); 3230 1.1 christos 3231 1.1 christos if ((input_section->flags & SEC_RELOC) != 0 3232 1.1 christos && input_section->reloc_count > 0) 3233 1.1 christos { 3234 1.1 christos asection **secpp; 3235 1.1 christos Elf_Internal_Sym *isym, *isymend; 3236 1.1 christos bfd_size_type amt; 3237 1.1 christos 3238 1.10 christos internal_relocs = (_bfd_elf_link_read_relocs 3239 1.1 christos (input_bfd, input_section, NULL, NULL, false)); 3240 1.8 christos if (internal_relocs == NULL) 3241 1.1 christos goto error_return; 3242 1.1 christos 3243 1.8 christos if (symtab_hdr->sh_info != 0) 3244 1.8 christos { 3245 1.8 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 3246 1.8 christos if (isymbuf == NULL) 3247 1.8 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3248 1.8 christos symtab_hdr->sh_info, 0, 3249 1.8 christos NULL, NULL, NULL); 3250 1.8 christos if (isymbuf == NULL) 3251 1.8 christos goto error_return; 3252 1.1 christos } 3253 1.1 christos 3254 1.1 christos amt = symtab_hdr->sh_info; 3255 1.1 christos amt *= sizeof (asection *); 3256 1.1 christos sections = bfd_malloc (amt); 3257 1.8 christos if (sections == NULL && amt != 0) 3258 1.1 christos goto error_return; 3259 1.1 christos 3260 1.1 christos isymend = isymbuf + symtab_hdr->sh_info; 3261 1.8 christos for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) 3262 1.8 christos { 3263 1.1 christos asection *isec; 3264 1.8 christos 3265 1.8 christos if (isym->st_shndx == SHN_UNDEF) 3266 1.8 christos isec = bfd_und_section_ptr; 3267 1.8 christos else if (isym->st_shndx == SHN_ABS) 3268 1.8 christos isec = bfd_abs_section_ptr; 3269 1.8 christos else if (isym->st_shndx == SHN_COMMON) 3270 1.8 christos isec = bfd_com_section_ptr; 3271 1.8 christos else 3272 1.1 christos isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 3273 1.8 christos 3274 1.8 christos *secpp = isec; 3275 1.1 christos } 3276 1.1 christos 3277 1.8 christos if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd, 3278 1.8 christos input_section, data, internal_relocs, 3279 1.8 christos isymbuf, sections)) 3280 1.1 christos goto error_return; 3281 1.9 christos 3282 1.9 christos free (sections); 3283 1.8 christos if (symtab_hdr->contents != (unsigned char *) isymbuf) 3284 1.1 christos free (isymbuf); 3285 1.8 christos if (elf_section_data (input_section)->relocs != internal_relocs) 3286 1.1 christos free (internal_relocs); 3287 1.1 christos } 3288 1.1 christos 3289 1.1 christos return data; 3290 1.1 christos 3291 1.9 christos error_return: 3292 1.9 christos free (sections); 3293 1.1 christos if (symtab_hdr->contents != (unsigned char *) isymbuf) 3294 1.9 christos free (isymbuf); 3295 1.1 christos if (elf_section_data (input_section)->relocs != internal_relocs) 3296 1.10 christos free (internal_relocs); 3297 1.10 christos if (orig_data == NULL) 3298 1.1 christos free (data); 3299 1.1 christos return NULL; 3300 1.1 christos } 3301 1.1 christos 3302 1.1 christos 3303 1.1 christos /* Determines the hash entry name for a particular reloc. It consists of 3304 1.1 christos the identifier of the symbol section and the added reloc addend and 3305 1.1 christos symbol offset relative to the section the symbol is attached to. */ 3306 1.1 christos 3307 1.1 christos static char * 3308 1.8 christos avr_stub_name (const asection *symbol_section, 3309 1.8 christos const bfd_vma symbol_offset, 3310 1.1 christos const Elf_Internal_Rela *rela) 3311 1.1 christos { 3312 1.1 christos char *stub_name; 3313 1.1 christos bfd_size_type len; 3314 1.1 christos 3315 1.1 christos len = 8 + 1 + 8 + 1 + 1; 3316 1.8 christos stub_name = bfd_malloc (len); 3317 1.8 christos if (stub_name != NULL) 3318 1.8 christos sprintf (stub_name, "%08x+%08x", 3319 1.8 christos symbol_section->id & 0xffffffff, 3320 1.1 christos (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset)); 3321 1.1 christos 3322 1.1 christos return stub_name; 3323 1.1 christos } 3324 1.1 christos 3325 1.1 christos 3326 1.1 christos /* Add a new stub entry to the stub hash. Not all fields of the new 3327 1.1 christos stub entry are initialised. */ 3328 1.1 christos 3329 1.1 christos static struct elf32_avr_stub_hash_entry * 3330 1.8 christos avr_add_stub (const char *stub_name, 3331 1.1 christos struct elf32_avr_link_hash_table *htab) 3332 1.1 christos { 3333 1.1 christos struct elf32_avr_stub_hash_entry *hsh; 3334 1.1 christos 3335 1.10 christos /* Enter this entry into the linker stub hash table. */ 3336 1.1 christos hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, true, false); 3337 1.1 christos 3338 1.1 christos if (hsh == NULL) 3339 1.7 christos { 3340 1.7 christos /* xgettext:c-format */ 3341 1.1 christos _bfd_error_handler (_("cannot create stub entry %s"), stub_name); 3342 1.1 christos return NULL; 3343 1.1 christos } 3344 1.1 christos 3345 1.1 christos hsh->stub_offset = 0; 3346 1.1 christos return hsh; 3347 1.1 christos } 3348 1.1 christos 3349 1.1 christos /* We assume that there is already space allocated for the stub section 3350 1.1 christos contents and that before building the stubs the section size is 3351 1.1 christos initialized to 0. We assume that within the stub hash table entry, 3352 1.1 christos the absolute position of the jmp target has been written in the 3353 1.1 christos target_value field. We write here the offset of the generated jmp insn 3354 1.1 christos relative to the trampoline section start to the stub_offset entry in 3355 1.1 christos the stub hash table entry. */ 3356 1.10 christos 3357 1.1 christos static bool 3358 1.1 christos avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg) 3359 1.1 christos { 3360 1.1 christos struct elf32_avr_stub_hash_entry *hsh; 3361 1.1 christos struct bfd_link_info *info; 3362 1.1 christos struct elf32_avr_link_hash_table *htab; 3363 1.1 christos bfd *stub_bfd; 3364 1.1 christos bfd_byte *loc; 3365 1.1 christos bfd_vma target; 3366 1.1 christos bfd_vma starget; 3367 1.1 christos 3368 1.1 christos /* Basic opcode */ 3369 1.1 christos bfd_vma jmp_insn = 0x0000940c; 3370 1.1 christos 3371 1.1 christos /* Massage our args to the form they really have. */ 3372 1.1 christos hsh = avr_stub_hash_entry (bh); 3373 1.1 christos 3374 1.10 christos if (!hsh->is_actually_needed) 3375 1.1 christos return true; 3376 1.1 christos 3377 1.1 christos info = (struct bfd_link_info *) in_arg; 3378 1.1 christos 3379 1.1 christos htab = avr_link_hash_table (info); 3380 1.10 christos if (htab == NULL) 3381 1.1 christos return false; 3382 1.1 christos 3383 1.1 christos target = hsh->target_value; 3384 1.1 christos 3385 1.1 christos /* Make a note of the offset within the stubs for this entry. */ 3386 1.1 christos hsh->stub_offset = htab->stub_sec->size; 3387 1.1 christos loc = htab->stub_sec->contents + hsh->stub_offset; 3388 1.1 christos 3389 1.1 christos stub_bfd = htab->stub_sec->owner; 3390 1.1 christos 3391 1.1 christos if (debug_stubs) 3392 1.8 christos printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n", 3393 1.8 christos (unsigned int) target, 3394 1.1 christos (unsigned int) hsh->stub_offset); 3395 1.1 christos 3396 1.1 christos /* We now have to add the information on the jump target to the bare 3397 1.1 christos opcode bits already set in jmp_insn. */ 3398 1.1 christos 3399 1.1 christos /* Check for the alignment of the address. */ 3400 1.10 christos if (target & 1) 3401 1.1 christos return false; 3402 1.1 christos 3403 1.1 christos starget = target >> 1; 3404 1.1 christos jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16; 3405 1.1 christos bfd_put_16 (stub_bfd, jmp_insn, loc); 3406 1.1 christos bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2); 3407 1.1 christos 3408 1.1 christos htab->stub_sec->size += 4; 3409 1.1 christos 3410 1.1 christos /* Now add the entries in the address mapping table if there is still 3411 1.1 christos space left. */ 3412 1.1 christos { 3413 1.1 christos unsigned int nr; 3414 1.1 christos 3415 1.1 christos nr = htab->amt_entry_cnt + 1; 3416 1.1 christos if (nr <= htab->amt_max_entry_cnt) 3417 1.8 christos { 3418 1.1 christos htab->amt_entry_cnt = nr; 3419 1.8 christos 3420 1.8 christos htab->amt_stub_offsets[nr - 1] = hsh->stub_offset; 3421 1.1 christos htab->amt_destination_addr[nr - 1] = target; 3422 1.1 christos } 3423 1.1 christos } 3424 1.10 christos 3425 1.1 christos return true; 3426 1.1 christos } 3427 1.10 christos 3428 1.1 christos static bool 3429 1.8 christos avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh, 3430 1.1 christos void *in_arg ATTRIBUTE_UNUSED) 3431 1.1 christos { 3432 1.1 christos struct elf32_avr_stub_hash_entry *hsh; 3433 1.1 christos 3434 1.10 christos hsh = avr_stub_hash_entry (bh); 3435 1.1 christos hsh->is_actually_needed = false; 3436 1.10 christos 3437 1.1 christos return true; 3438 1.1 christos } 3439 1.10 christos 3440 1.1 christos static bool 3441 1.1 christos avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg) 3442 1.1 christos { 3443 1.1 christos struct elf32_avr_stub_hash_entry *hsh; 3444 1.1 christos struct elf32_avr_link_hash_table *htab; 3445 1.1 christos int size; 3446 1.1 christos 3447 1.1 christos /* Massage our args to the form they really have. */ 3448 1.1 christos hsh = avr_stub_hash_entry (bh); 3449 1.1 christos htab = in_arg; 3450 1.1 christos 3451 1.1 christos if (hsh->is_actually_needed) 3452 1.1 christos size = 4; 3453 1.1 christos else 3454 1.1 christos size = 0; 3455 1.1 christos 3456 1.10 christos htab->stub_sec->size += size; 3457 1.1 christos return true; 3458 1.1 christos } 3459 1.1 christos 3460 1.1 christos void 3461 1.8 christos elf32_avr_setup_params (struct bfd_link_info *info, 3462 1.8 christos bfd *avr_stub_bfd, 3463 1.10 christos asection *avr_stub_section, 3464 1.10 christos bool no_stubs, 3465 1.10 christos bool deb_stubs, 3466 1.8 christos bool deb_relax, 3467 1.10 christos bfd_vma pc_wrap_around, 3468 1.1 christos bool call_ret_replacement) 3469 1.1 christos { 3470 1.1 christos struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3471 1.1 christos 3472 1.1 christos if (htab == NULL) 3473 1.1 christos return; 3474 1.1 christos htab->stub_sec = avr_stub_section; 3475 1.1 christos htab->stub_bfd = avr_stub_bfd; 3476 1.1 christos htab->no_stubs = no_stubs; 3477 1.1 christos 3478 1.1 christos debug_relax = deb_relax; 3479 1.1 christos debug_stubs = deb_stubs; 3480 1.1 christos avr_pc_wrap_around = pc_wrap_around; 3481 1.1 christos avr_replace_call_ret_sequences = call_ret_replacement; 3482 1.1 christos } 3483 1.1 christos 3484 1.1 christos 3485 1.1 christos /* Set up various things so that we can make a list of input sections 3486 1.1 christos for each output section included in the link. Returns -1 on error, 3487 1.1 christos 0 when no stubs will be needed, and 1 on success. It also sets 3488 1.1 christos information on the stubs bfd and the stub section in the info 3489 1.1 christos struct. */ 3490 1.1 christos 3491 1.1 christos int 3492 1.8 christos elf32_avr_setup_section_lists (bfd *output_bfd, 3493 1.1 christos struct bfd_link_info *info) 3494 1.1 christos { 3495 1.1 christos bfd *input_bfd; 3496 1.6 christos unsigned int bfd_count; 3497 1.1 christos unsigned int top_id, top_index; 3498 1.1 christos asection *section; 3499 1.9 christos asection **input_list, **list; 3500 1.1 christos size_t amt; 3501 1.1 christos struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3502 1.1 christos 3503 1.1 christos if (htab == NULL || htab->no_stubs) 3504 1.1 christos return 0; 3505 1.1 christos 3506 1.1 christos /* Count the number of input BFDs and find the top input section id. */ 3507 1.1 christos for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0; 3508 1.3 christos input_bfd != NULL; 3509 1.1 christos input_bfd = input_bfd->link.next) 3510 1.1 christos { 3511 1.1 christos bfd_count += 1; 3512 1.8 christos for (section = input_bfd->sections; 3513 1.8 christos section != NULL; 3514 1.1 christos section = section->next) 3515 1.1 christos if (top_id < section->id) 3516 1.1 christos top_id = section->id; 3517 1.1 christos } 3518 1.1 christos 3519 1.1 christos htab->bfd_count = bfd_count; 3520 1.1 christos 3521 1.1 christos /* We can't use output_bfd->section_count here to find the top output 3522 1.1 christos section index as some sections may have been removed, and 3523 1.1 christos strip_excluded_output_sections doesn't renumber the indices. */ 3524 1.1 christos for (section = output_bfd->sections, top_index = 0; 3525 1.1 christos section != NULL; 3526 1.1 christos section = section->next) 3527 1.1 christos if (top_index < section->index) 3528 1.1 christos top_index = section->index; 3529 1.1 christos 3530 1.1 christos htab->top_index = top_index; 3531 1.1 christos amt = sizeof (asection *) * (top_index + 1); 3532 1.1 christos input_list = bfd_malloc (amt); 3533 1.1 christos htab->input_list = input_list; 3534 1.1 christos if (input_list == NULL) 3535 1.1 christos return -1; 3536 1.1 christos 3537 1.1 christos /* For sections we aren't interested in, mark their entries with a 3538 1.1 christos value we can check later. */ 3539 1.1 christos list = input_list + top_index; 3540 1.1 christos do 3541 1.1 christos *list = bfd_abs_section_ptr; 3542 1.1 christos while (list-- != input_list); 3543 1.1 christos 3544 1.1 christos for (section = output_bfd->sections; 3545 1.1 christos section != NULL; 3546 1.1 christos section = section->next) 3547 1.1 christos if ((section->flags & SEC_CODE) != 0) 3548 1.1 christos input_list[section->index] = NULL; 3549 1.1 christos 3550 1.1 christos return 1; 3551 1.1 christos } 3552 1.1 christos 3553 1.1 christos 3554 1.1 christos /* Read in all local syms for all input bfds, and create hash entries 3555 1.1 christos for export stubs if we are building a multi-subspace shared lib. 3556 1.1 christos Returns -1 on error, 0 otherwise. */ 3557 1.1 christos 3558 1.1 christos static int 3559 1.1 christos get_local_syms (bfd *input_bfd, struct bfd_link_info *info) 3560 1.1 christos { 3561 1.1 christos unsigned int bfd_indx; 3562 1.1 christos Elf_Internal_Sym *local_syms, **all_local_syms; 3563 1.9 christos struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3564 1.1 christos size_t amt; 3565 1.1 christos 3566 1.1 christos if (htab == NULL) 3567 1.1 christos return -1; 3568 1.1 christos 3569 1.1 christos /* We want to read in symbol extension records only once. To do this 3570 1.1 christos we need to read in the local symbols in parallel and save them for 3571 1.1 christos later use; so hold pointers to the local symbols in an array. */ 3572 1.1 christos amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count; 3573 1.1 christos all_local_syms = bfd_zmalloc (amt); 3574 1.1 christos htab->all_local_syms = all_local_syms; 3575 1.1 christos if (all_local_syms == NULL) 3576 1.1 christos return -1; 3577 1.1 christos 3578 1.1 christos /* Walk over all the input BFDs, swapping in local symbols. 3579 1.1 christos If we are creating a shared library, create hash entries for the 3580 1.1 christos export stubs. */ 3581 1.1 christos for (bfd_indx = 0; 3582 1.3 christos input_bfd != NULL; 3583 1.1 christos input_bfd = input_bfd->link.next, bfd_indx++) 3584 1.1 christos { 3585 1.1 christos Elf_Internal_Shdr *symtab_hdr; 3586 1.1 christos 3587 1.1 christos /* We'll need the symbol table in a second. */ 3588 1.1 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3589 1.1 christos if (symtab_hdr->sh_info == 0) 3590 1.1 christos continue; 3591 1.1 christos 3592 1.1 christos /* We need an array of the local symbols attached to the input bfd. */ 3593 1.1 christos local_syms = (Elf_Internal_Sym *) symtab_hdr->contents; 3594 1.1 christos if (local_syms == NULL) 3595 1.1 christos { 3596 1.1 christos local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3597 1.1 christos symtab_hdr->sh_info, 0, 3598 1.1 christos NULL, NULL, NULL); 3599 1.1 christos /* Cache them for elf_link_input_bfd. */ 3600 1.1 christos symtab_hdr->contents = (unsigned char *) local_syms; 3601 1.1 christos } 3602 1.1 christos if (local_syms == NULL) 3603 1.1 christos return -1; 3604 1.1 christos 3605 1.1 christos all_local_syms[bfd_indx] = local_syms; 3606 1.1 christos } 3607 1.1 christos 3608 1.1 christos return 0; 3609 1.1 christos } 3610 1.1 christos 3611 1.1 christos #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0 3612 1.10 christos 3613 1.1 christos bool 3614 1.8 christos elf32_avr_size_stubs (bfd *output_bfd, 3615 1.10 christos struct bfd_link_info *info, 3616 1.1 christos bool is_prealloc_run) 3617 1.1 christos { 3618 1.1 christos struct elf32_avr_link_hash_table *htab; 3619 1.1 christos int stub_changed = 0; 3620 1.1 christos 3621 1.1 christos htab = avr_link_hash_table (info); 3622 1.10 christos if (htab == NULL) 3623 1.1 christos return false; 3624 1.1 christos 3625 1.1 christos /* At this point we initialize htab->vector_base 3626 1.1 christos To the start of the text output section. */ 3627 1.1 christos htab->vector_base = htab->stub_sec->output_section->vma; 3628 1.1 christos 3629 1.1 christos if (get_local_syms (info->input_bfds, info)) 3630 1.1 christos { 3631 1.1 christos if (htab->all_local_syms) 3632 1.10 christos goto error_ret_free_local; 3633 1.1 christos return false; 3634 1.1 christos } 3635 1.1 christos 3636 1.1 christos if (ADD_DUMMY_STUBS_FOR_DEBUGGING) 3637 1.1 christos { 3638 1.1 christos struct elf32_avr_stub_hash_entry *test; 3639 1.1 christos 3640 1.1 christos test = avr_add_stub ("Hugo",htab); 3641 1.1 christos test->target_value = 0x123456; 3642 1.1 christos test->stub_offset = 13; 3643 1.1 christos 3644 1.1 christos test = avr_add_stub ("Hugo2",htab); 3645 1.1 christos test->target_value = 0x84210; 3646 1.1 christos test->stub_offset = 14; 3647 1.1 christos } 3648 1.1 christos 3649 1.1 christos while (1) 3650 1.1 christos { 3651 1.1 christos bfd *input_bfd; 3652 1.1 christos unsigned int bfd_indx; 3653 1.1 christos 3654 1.8 christos /* We will have to re-generate the stub hash table each time anything 3655 1.1 christos in memory has changed. */ 3656 1.1 christos 3657 1.1 christos bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab); 3658 1.8 christos for (input_bfd = info->input_bfds, bfd_indx = 0; 3659 1.8 christos input_bfd != NULL; 3660 1.8 christos input_bfd = input_bfd->link.next, bfd_indx++) 3661 1.8 christos { 3662 1.8 christos Elf_Internal_Shdr *symtab_hdr; 3663 1.8 christos asection *section; 3664 1.8 christos Elf_Internal_Sym *local_syms; 3665 1.8 christos 3666 1.8 christos /* We'll need the symbol table in a second. */ 3667 1.8 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3668 1.8 christos if (symtab_hdr->sh_info == 0) 3669 1.8 christos continue; 3670 1.8 christos 3671 1.8 christos local_syms = htab->all_local_syms[bfd_indx]; 3672 1.8 christos 3673 1.8 christos /* Walk over each section attached to the input bfd. */ 3674 1.8 christos for (section = input_bfd->sections; 3675 1.8 christos section != NULL; 3676 1.8 christos section = section->next) 3677 1.8 christos { 3678 1.8 christos Elf_Internal_Rela *internal_relocs, *irelaend, *irela; 3679 1.8 christos 3680 1.8 christos /* If there aren't any relocs, then there's nothing more 3681 1.8 christos to do. */ 3682 1.8 christos if ((section->flags & SEC_RELOC) == 0 3683 1.8 christos || section->reloc_count == 0) 3684 1.8 christos continue; 3685 1.8 christos 3686 1.8 christos /* If this section is a link-once section that will be 3687 1.8 christos discarded, then don't create any stubs. */ 3688 1.8 christos if (section->output_section == NULL 3689 1.8 christos || section->output_section->owner != output_bfd) 3690 1.8 christos continue; 3691 1.8 christos 3692 1.8 christos /* Get the relocs. */ 3693 1.8 christos internal_relocs 3694 1.8 christos = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL, 3695 1.8 christos info->keep_memory); 3696 1.8 christos if (internal_relocs == NULL) 3697 1.8 christos goto error_ret_free_local; 3698 1.8 christos 3699 1.8 christos /* Now examine each relocation. */ 3700 1.8 christos irela = internal_relocs; 3701 1.8 christos irelaend = irela + section->reloc_count; 3702 1.8 christos for (; irela < irelaend; irela++) 3703 1.8 christos { 3704 1.8 christos unsigned int r_type, r_indx; 3705 1.8 christos struct elf32_avr_stub_hash_entry *hsh; 3706 1.8 christos asection *sym_sec; 3707 1.8 christos bfd_vma sym_value; 3708 1.8 christos bfd_vma destination; 3709 1.8 christos struct elf_link_hash_entry *hh; 3710 1.8 christos char *stub_name; 3711 1.8 christos 3712 1.8 christos r_type = ELF32_R_TYPE (irela->r_info); 3713 1.8 christos r_indx = ELF32_R_SYM (irela->r_info); 3714 1.8 christos 3715 1.8 christos /* Only look for 16 bit GS relocs. No other reloc will need a 3716 1.8 christos stub. */ 3717 1.8 christos if (!((r_type == R_AVR_16_PM) 3718 1.8 christos || (r_type == R_AVR_LO8_LDI_GS) 3719 1.8 christos || (r_type == R_AVR_HI8_LDI_GS))) 3720 1.8 christos continue; 3721 1.8 christos 3722 1.8 christos /* Now determine the call target, its name, value, 3723 1.8 christos section. */ 3724 1.8 christos sym_sec = NULL; 3725 1.8 christos sym_value = 0; 3726 1.8 christos destination = 0; 3727 1.8 christos hh = NULL; 3728 1.8 christos if (r_indx < symtab_hdr->sh_info) 3729 1.8 christos { 3730 1.8 christos /* It's a local symbol. */ 3731 1.8 christos Elf_Internal_Sym *sym; 3732 1.1 christos Elf_Internal_Shdr *hdr; 3733 1.1 christos unsigned int shndx; 3734 1.8 christos 3735 1.8 christos sym = local_syms + r_indx; 3736 1.8 christos if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) 3737 1.1 christos sym_value = sym->st_value; 3738 1.1 christos shndx = sym->st_shndx; 3739 1.1 christos if (shndx < elf_numsections (input_bfd)) 3740 1.1 christos { 3741 1.1 christos hdr = elf_elfsections (input_bfd)[shndx]; 3742 1.1 christos sym_sec = hdr->bfd_section; 3743 1.1 christos destination = (sym_value + irela->r_addend 3744 1.1 christos + sym_sec->output_offset 3745 1.1 christos + sym_sec->output_section->vma); 3746 1.8 christos } 3747 1.8 christos } 3748 1.8 christos else 3749 1.8 christos { 3750 1.8 christos /* It's an external symbol. */ 3751 1.8 christos int e_indx; 3752 1.8 christos 3753 1.8 christos e_indx = r_indx - symtab_hdr->sh_info; 3754 1.8 christos hh = elf_sym_hashes (input_bfd)[e_indx]; 3755 1.8 christos 3756 1.8 christos while (hh->root.type == bfd_link_hash_indirect 3757 1.8 christos || hh->root.type == bfd_link_hash_warning) 3758 1.8 christos hh = (struct elf_link_hash_entry *) 3759 1.1 christos (hh->root.u.i.link); 3760 1.8 christos 3761 1.8 christos if (hh->root.type == bfd_link_hash_defined 3762 1.8 christos || hh->root.type == bfd_link_hash_defweak) 3763 1.8 christos { 3764 1.8 christos sym_sec = hh->root.u.def.section; 3765 1.8 christos sym_value = hh->root.u.def.value; 3766 1.8 christos if (sym_sec->output_section != NULL) 3767 1.8 christos destination = (sym_value + irela->r_addend 3768 1.8 christos + sym_sec->output_offset 3769 1.8 christos + sym_sec->output_section->vma); 3770 1.8 christos } 3771 1.8 christos else if (hh->root.type == bfd_link_hash_undefweak) 3772 1.8 christos { 3773 1.8 christos if (! bfd_link_pic (info)) 3774 1.8 christos continue; 3775 1.8 christos } 3776 1.8 christos else if (hh->root.type == bfd_link_hash_undefined) 3777 1.8 christos { 3778 1.8 christos if (! (info->unresolved_syms_in_objects == RM_IGNORE 3779 1.8 christos && (ELF_ST_VISIBILITY (hh->other) 3780 1.8 christos == STV_DEFAULT))) 3781 1.8 christos continue; 3782 1.8 christos } 3783 1.8 christos else 3784 1.8 christos { 3785 1.8 christos bfd_set_error (bfd_error_bad_value); 3786 1.8 christos 3787 1.8 christos error_ret_free_internal: 3788 1.8 christos if (elf_section_data (section)->relocs == NULL) 3789 1.8 christos free (internal_relocs); 3790 1.8 christos goto error_ret_free_local; 3791 1.8 christos } 3792 1.8 christos } 3793 1.8 christos 3794 1.1 christos if (! avr_stub_is_required_for_16_bit_reloc 3795 1.8 christos (destination - htab->vector_base)) 3796 1.8 christos { 3797 1.1 christos if (!is_prealloc_run) 3798 1.1 christos /* We are having a reloc that does't need a stub. */ 3799 1.1 christos continue; 3800 1.1 christos 3801 1.1 christos /* We don't right now know if a stub will be needed. 3802 1.8 christos Let's rather be on the safe side. */ 3803 1.1 christos } 3804 1.8 christos 3805 1.8 christos /* Get the name of this stub. */ 3806 1.1 christos stub_name = avr_stub_name (sym_sec, sym_value, irela); 3807 1.8 christos 3808 1.8 christos if (!stub_name) 3809 1.1 christos goto error_ret_free_internal; 3810 1.1 christos 3811 1.8 christos 3812 1.8 christos hsh = avr_stub_hash_lookup (&htab->bstab, 3813 1.10 christos stub_name, 3814 1.8 christos false, false); 3815 1.8 christos if (hsh != NULL) 3816 1.8 christos { 3817 1.8 christos /* The proper stub has already been created. Mark it 3818 1.8 christos to be used and write the possibly changed destination 3819 1.10 christos value. */ 3820 1.8 christos hsh->is_actually_needed = true; 3821 1.8 christos hsh->target_value = destination; 3822 1.8 christos free (stub_name); 3823 1.8 christos continue; 3824 1.8 christos } 3825 1.8 christos 3826 1.8 christos hsh = avr_add_stub (stub_name, htab); 3827 1.8 christos if (hsh == NULL) 3828 1.8 christos { 3829 1.8 christos free (stub_name); 3830 1.8 christos goto error_ret_free_internal; 3831 1.8 christos } 3832 1.10 christos 3833 1.8 christos hsh->is_actually_needed = true; 3834 1.8 christos hsh->target_value = destination; 3835 1.8 christos 3836 1.8 christos if (debug_stubs) 3837 1.8 christos printf ("Adding stub with destination 0x%x to the" 3838 1.8 christos " hash table.\n", (unsigned int) destination); 3839 1.8 christos if (debug_stubs) 3840 1.8 christos printf ("(Pre-Alloc run: %i)\n", is_prealloc_run); 3841 1.10 christos 3842 1.8 christos stub_changed = true; 3843 1.8 christos } 3844 1.8 christos 3845 1.8 christos /* We're done with the internal relocs, free them. */ 3846 1.8 christos if (elf_section_data (section)->relocs == NULL) 3847 1.8 christos free (internal_relocs); 3848 1.8 christos } 3849 1.1 christos } 3850 1.1 christos 3851 1.1 christos /* Re-Calculate the number of needed stubs. */ 3852 1.1 christos htab->stub_sec->size = 0; 3853 1.1 christos bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab); 3854 1.1 christos 3855 1.8 christos if (!stub_changed) 3856 1.1 christos break; 3857 1.10 christos 3858 1.1 christos stub_changed = false; 3859 1.1 christos } 3860 1.1 christos 3861 1.10 christos free (htab->all_local_syms); 3862 1.1 christos return true; 3863 1.1 christos 3864 1.1 christos error_ret_free_local: 3865 1.10 christos free (htab->all_local_syms); 3866 1.1 christos return false; 3867 1.1 christos } 3868 1.1 christos 3869 1.1 christos 3870 1.1 christos /* Build all the stubs associated with the current output file. The 3871 1.1 christos stubs are kept in a hash table attached to the main linker hash 3872 1.1 christos table. We also set up the .plt entries for statically linked PIC 3873 1.1 christos functions here. This function is called via hppaelf_finish in the 3874 1.1 christos linker. */ 3875 1.10 christos 3876 1.1 christos bool 3877 1.1 christos elf32_avr_build_stubs (struct bfd_link_info *info) 3878 1.1 christos { 3879 1.1 christos asection *stub_sec; 3880 1.1 christos struct bfd_hash_table *table; 3881 1.1 christos struct elf32_avr_link_hash_table *htab; 3882 1.1 christos bfd_size_type total_size = 0; 3883 1.1 christos 3884 1.1 christos htab = avr_link_hash_table (info); 3885 1.10 christos if (htab == NULL) 3886 1.1 christos return false; 3887 1.1 christos 3888 1.1 christos /* In case that there were several stub sections: */ 3889 1.1 christos for (stub_sec = htab->stub_bfd->sections; 3890 1.1 christos stub_sec != NULL; 3891 1.1 christos stub_sec = stub_sec->next) 3892 1.1 christos { 3893 1.1 christos bfd_size_type size; 3894 1.1 christos 3895 1.1 christos /* Allocate memory to hold the linker stubs. */ 3896 1.1 christos size = stub_sec->size; 3897 1.1 christos total_size += size; 3898 1.1 christos 3899 1.1 christos stub_sec->contents = bfd_zalloc (htab->stub_bfd, size); 3900 1.10 christos if (stub_sec->contents == NULL && size != 0) 3901 1.1 christos return false; 3902 1.1 christos stub_sec->size = 0; 3903 1.1 christos } 3904 1.1 christos 3905 1.1 christos /* Allocate memory for the adress mapping table. */ 3906 1.1 christos htab->amt_entry_cnt = 0; 3907 1.1 christos htab->amt_max_entry_cnt = total_size / 4; 3908 1.8 christos htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma) 3909 1.1 christos * htab->amt_max_entry_cnt); 3910 1.1 christos htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma) 3911 1.1 christos * htab->amt_max_entry_cnt ); 3912 1.1 christos 3913 1.1 christos if (debug_stubs) 3914 1.1 christos printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt); 3915 1.1 christos 3916 1.1 christos /* Build the stubs as directed by the stub hash table. */ 3917 1.1 christos table = &htab->bstab; 3918 1.1 christos bfd_hash_traverse (table, avr_build_one_stub, info); 3919 1.1 christos 3920 1.1 christos if (debug_stubs) 3921 1.1 christos printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size); 3922 1.10 christos 3923 1.1 christos return true; 3924 1.1 christos } 3925 1.5 christos 3926 1.5 christos /* Callback used by QSORT to order relocations AP and BP. */ 3927 1.5 christos 3928 1.5 christos static int 3929 1.5 christos internal_reloc_compare (const void *ap, const void *bp) 3930 1.5 christos { 3931 1.5 christos const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap; 3932 1.5 christos const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp; 3933 1.5 christos 3934 1.5 christos if (a->r_offset != b->r_offset) 3935 1.5 christos return (a->r_offset - b->r_offset); 3936 1.5 christos 3937 1.5 christos /* We don't need to sort on these criteria for correctness, 3938 1.5 christos but enforcing a more strict ordering prevents unstable qsort 3939 1.5 christos from behaving differently with different implementations. 3940 1.5 christos Without the code below we get correct but different results 3941 1.5 christos on Solaris 2.7 and 2.8. We would like to always produce the 3942 1.5 christos same results no matter the host. */ 3943 1.5 christos 3944 1.5 christos if (a->r_info != b->r_info) 3945 1.5 christos return (a->r_info - b->r_info); 3946 1.5 christos 3947 1.5 christos return (a->r_addend - b->r_addend); 3948 1.5 christos } 3949 1.5 christos 3950 1.5 christos /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */ 3951 1.10 christos 3952 1.9 christos static bool 3953 1.5 christos avr_is_section_for_address (asection *section, bfd_vma address) 3954 1.5 christos { 3955 1.5 christos bfd_vma vma; 3956 1.5 christos bfd_size_type size; 3957 1.9 christos 3958 1.5 christos vma = bfd_section_vma (section); 3959 1.10 christos if (address < vma) 3960 1.5 christos return false; 3961 1.5 christos 3962 1.5 christos size = section->size; 3963 1.10 christos if (address >= vma + size) 3964 1.5 christos return false; 3965 1.10 christos 3966 1.5 christos return true; 3967 1.5 christos } 3968 1.5 christos 3969 1.5 christos /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */ 3970 1.5 christos 3971 1.5 christos struct avr_find_section_data 3972 1.5 christos { 3973 1.5 christos /* The address we're looking for. */ 3974 1.5 christos bfd_vma address; 3975 1.5 christos 3976 1.5 christos /* The section we've found. */ 3977 1.5 christos asection *section; 3978 1.5 christos }; 3979 1.5 christos 3980 1.5 christos /* Helper function to locate the section holding a certain virtual memory 3981 1.5 christos address. This is called via bfd_map_over_sections. The DATA is an 3982 1.5 christos instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which 3983 1.5 christos has been set to the address to search for, and the section field has 3984 1.5 christos been set to NULL. If SECTION from ABFD contains ADDRESS then the 3985 1.5 christos section field in DATA will be set to SECTION. As an optimisation, if 3986 1.5 christos the section field is already non-null then this function does not 3987 1.5 christos perform any checks, and just returns. */ 3988 1.5 christos 3989 1.9 christos static void 3990 1.8 christos avr_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED, 3991 1.5 christos asection *section, void *data) 3992 1.5 christos { 3993 1.5 christos struct avr_find_section_data *fs_data 3994 1.5 christos = (struct avr_find_section_data *) data; 3995 1.5 christos 3996 1.5 christos /* Return if already found. */ 3997 1.5 christos if (fs_data->section != NULL) 3998 1.5 christos return; 3999 1.5 christos 4000 1.9 christos /* If this section isn't part of the addressable code content, skip it. */ 4001 1.9 christos if ((bfd_section_flags (section) & SEC_ALLOC) == 0 4002 1.5 christos && (bfd_section_flags (section) & SEC_CODE) == 0) 4003 1.5 christos return; 4004 1.9 christos 4005 1.5 christos if (avr_is_section_for_address (section, fs_data->address)) 4006 1.5 christos fs_data->section = section; 4007 1.5 christos } 4008 1.5 christos 4009 1.5 christos /* Load all of the property records from SEC, a section from ABFD. Return 4010 1.5 christos a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The 4011 1.5 christos memory for the returned structure, and all of the records pointed too by 4012 1.5 christos the structure are allocated with a single call to malloc, so, only the 4013 1.5 christos pointer returned needs to be free'd. */ 4014 1.5 christos 4015 1.5 christos static struct avr_property_record_list * 4016 1.5 christos avr_elf32_load_records_from_section (bfd *abfd, asection *sec) 4017 1.10 christos { 4018 1.5 christos bfd_byte *contents, *ptr; 4019 1.5 christos bfd_size_type size, mem_size; 4020 1.5 christos bfd_byte version, flags; 4021 1.5 christos uint16_t record_count, i; 4022 1.5 christos struct avr_property_record_list *r_list = NULL; 4023 1.5 christos Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end; 4024 1.5 christos struct avr_find_section_data fs_data; 4025 1.5 christos 4026 1.5 christos fs_data.section = NULL; 4027 1.10 christos 4028 1.10 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 4029 1.5 christos goto load_failed; 4030 1.5 christos ptr = contents; 4031 1.5 christos 4032 1.5 christos /* Load the relocations for the '.avr.prop' section if there are any, and 4033 1.5 christos sort them. */ 4034 1.10 christos internal_relocs = (_bfd_elf_link_read_relocs 4035 1.5 christos (abfd, sec, NULL, NULL, false)); 4036 1.5 christos if (internal_relocs) 4037 1.8 christos qsort (internal_relocs, sec->reloc_count, 4038 1.5 christos sizeof (Elf_Internal_Rela), internal_reloc_compare); 4039 1.5 christos 4040 1.5 christos /* There is a header at the start of the property record section SEC, the 4041 1.5 christos format of this header is: 4042 1.5 christos uint8_t : version number 4043 1.5 christos uint8_t : flags 4044 1.5 christos uint16_t : record counter 4045 1.5 christos */ 4046 1.5 christos 4047 1.10 christos /* Check we have at least got a headers worth of bytes. */ 4048 1.5 christos size = bfd_section_size (sec); 4049 1.5 christos if (size < AVR_PROPERTY_SECTION_HEADER_SIZE) 4050 1.5 christos goto load_failed; 4051 1.10 christos 4052 1.5 christos version = *ptr; 4053 1.10 christos ptr++; 4054 1.5 christos flags = *ptr; 4055 1.9 christos ptr++; 4056 1.10 christos record_count = bfd_get_16 (abfd, ptr); 4057 1.5 christos ptr += 2; 4058 1.5 christos BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE); 4059 1.5 christos 4060 1.5 christos /* Now allocate space for the list structure, and all of the list 4061 1.5 christos elements in a single block. */ 4062 1.5 christos mem_size = sizeof (struct avr_property_record_list) 4063 1.5 christos + sizeof (struct avr_property_record) * record_count; 4064 1.5 christos r_list = bfd_malloc (mem_size); 4065 1.5 christos if (r_list == NULL) 4066 1.5 christos goto load_failed; 4067 1.5 christos 4068 1.5 christos r_list->version = version; 4069 1.5 christos r_list->flags = flags; 4070 1.5 christos r_list->section = sec; 4071 1.5 christos r_list->record_count = record_count; 4072 1.5 christos r_list->records = (struct avr_property_record *) (&r_list [1]); 4073 1.5 christos size -= AVR_PROPERTY_SECTION_HEADER_SIZE; 4074 1.5 christos 4075 1.5 christos /* Check that we understand the version number. There is only one 4076 1.5 christos version number right now, anything else is an error. */ 4077 1.5 christos if (r_list->version != AVR_PROPERTY_RECORDS_VERSION) 4078 1.5 christos goto load_failed; 4079 1.5 christos 4080 1.5 christos rel = internal_relocs; 4081 1.5 christos rel_end = rel + sec->reloc_count; 4082 1.5 christos for (i = 0; i < record_count; ++i) 4083 1.5 christos { 4084 1.5 christos bfd_vma address; 4085 1.5 christos 4086 1.8 christos /* Each entry is a 32-bit address, followed by a single byte type. 4087 1.8 christos After that is the type specific data. We must take care to 4088 1.5 christos ensure that we don't read beyond the end of the section data. */ 4089 1.8 christos if (size < 5) 4090 1.5 christos goto load_failed; 4091 1.5 christos 4092 1.5 christos r_list->records [i].section = NULL; 4093 1.5 christos r_list->records [i].offset = 0; 4094 1.5 christos 4095 1.8 christos if (rel) 4096 1.8 christos { 4097 1.8 christos /* The offset of the address within the .avr.prop section. */ 4098 1.8 christos size_t offset = ptr - contents; 4099 1.8 christos 4100 1.8 christos while (rel < rel_end && rel->r_offset < offset) 4101 1.8 christos ++rel; 4102 1.8 christos 4103 1.8 christos if (rel == rel_end) 4104 1.8 christos rel = NULL; 4105 1.8 christos else if (rel->r_offset == offset) 4106 1.8 christos { 4107 1.8 christos /* Find section and section offset. */ 4108 1.8 christos unsigned long r_symndx; 4109 1.8 christos 4110 1.8 christos asection * rel_sec; 4111 1.8 christos bfd_vma sec_offset; 4112 1.8 christos 4113 1.8 christos r_symndx = ELF32_R_SYM (rel->r_info); 4114 1.8 christos rel_sec = get_elf_r_symndx_section (abfd, r_symndx); 4115 1.8 christos sec_offset = get_elf_r_symndx_offset (abfd, r_symndx) 4116 1.8 christos + rel->r_addend; 4117 1.8 christos 4118 1.8 christos r_list->records [i].section = rel_sec; 4119 1.8 christos r_list->records [i].offset = sec_offset; 4120 1.8 christos } 4121 1.5 christos } 4122 1.9 christos 4123 1.5 christos address = bfd_get_32 (abfd, ptr); 4124 1.5 christos ptr += 4; 4125 1.5 christos size -= 4; 4126 1.5 christos 4127 1.8 christos if (r_list->records [i].section == NULL) 4128 1.8 christos { 4129 1.8 christos /* Try to find section and offset from address. */ 4130 1.9 christos if (fs_data.section != NULL 4131 1.8 christos && !avr_is_section_for_address (fs_data.section, address)) 4132 1.8 christos fs_data.section = NULL; 4133 1.8 christos 4134 1.8 christos if (fs_data.section == NULL) 4135 1.8 christos { 4136 1.8 christos fs_data.address = address; 4137 1.8 christos bfd_map_over_sections (abfd, avr_find_section_for_address, 4138 1.8 christos &fs_data); 4139 1.8 christos } 4140 1.8 christos 4141 1.8 christos if (fs_data.section == NULL) 4142 1.8 christos { 4143 1.8 christos fprintf (stderr, "Failed to find matching section.\n"); 4144 1.8 christos goto load_failed; 4145 1.8 christos } 4146 1.8 christos 4147 1.8 christos r_list->records [i].section = fs_data.section; 4148 1.9 christos r_list->records [i].offset 4149 1.8 christos = address - bfd_section_vma (fs_data.section); 4150 1.5 christos } 4151 1.10 christos 4152 1.5 christos r_list->records [i].type = *ptr; 4153 1.5 christos ptr += 1; 4154 1.5 christos size -= 1; 4155 1.5 christos 4156 1.8 christos switch (r_list->records [i].type) 4157 1.8 christos { 4158 1.8 christos case RECORD_ORG: 4159 1.8 christos /* Nothing else to load. */ 4160 1.8 christos break; 4161 1.8 christos case RECORD_ORG_AND_FILL: 4162 1.8 christos /* Just a 4-byte fill to load. */ 4163 1.8 christos if (size < 4) 4164 1.9 christos goto load_failed; 4165 1.8 christos r_list->records [i].data.org.fill = bfd_get_32 (abfd, ptr); 4166 1.8 christos ptr += 4; 4167 1.8 christos size -= 4; 4168 1.8 christos break; 4169 1.8 christos case RECORD_ALIGN: 4170 1.8 christos /* Just a 4-byte alignment to load. */ 4171 1.8 christos if (size < 4) 4172 1.9 christos goto load_failed; 4173 1.8 christos r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr); 4174 1.8 christos ptr += 4; 4175 1.8 christos size -= 4; 4176 1.8 christos /* Just initialise PRECEDING_DELETED field, this field is 4177 1.8 christos used during linker relaxation. */ 4178 1.8 christos r_list->records [i].data.align.preceding_deleted = 0; 4179 1.8 christos break; 4180 1.8 christos case RECORD_ALIGN_AND_FILL: 4181 1.8 christos /* A 4-byte alignment, and a 4-byte fill to load. */ 4182 1.8 christos if (size < 8) 4183 1.9 christos goto load_failed; 4184 1.8 christos r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr); 4185 1.9 christos ptr += 4; 4186 1.8 christos r_list->records [i].data.align.fill = bfd_get_32 (abfd, ptr); 4187 1.8 christos ptr += 4; 4188 1.8 christos size -= 8; 4189 1.8 christos /* Just initialise PRECEDING_DELETED field, this field is 4190 1.8 christos used during linker relaxation. */ 4191 1.8 christos r_list->records [i].data.align.preceding_deleted = 0; 4192 1.8 christos break; 4193 1.8 christos default: 4194 1.8 christos goto load_failed; 4195 1.5 christos } 4196 1.5 christos } 4197 1.5 christos 4198 1.6 christos free (contents); 4199 1.6 christos if (elf_section_data (sec)->relocs != internal_relocs) 4200 1.5 christos free (internal_relocs); 4201 1.5 christos return r_list; 4202 1.5 christos 4203 1.6 christos load_failed: 4204 1.6 christos if (elf_section_data (sec)->relocs != internal_relocs) 4205 1.5 christos free (internal_relocs); 4206 1.5 christos free (contents); 4207 1.5 christos free (r_list); 4208 1.5 christos return NULL; 4209 1.5 christos } 4210 1.5 christos 4211 1.5 christos /* Load all of the property records from ABFD. See 4212 1.5 christos AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */ 4213 1.5 christos 4214 1.5 christos struct avr_property_record_list * 4215 1.5 christos avr_elf32_load_property_records (bfd *abfd) 4216 1.5 christos { 4217 1.5 christos asection *sec; 4218 1.5 christos 4219 1.5 christos /* Find the '.avr.prop' section and load the contents into memory. */ 4220 1.11 christos sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME); 4221 1.5 christos if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0) 4222 1.5 christos return NULL; 4223 1.5 christos return avr_elf32_load_records_from_section (abfd, sec); 4224 1.5 christos } 4225 1.5 christos 4226 1.5 christos const char * 4227 1.5 christos avr_elf32_property_record_name (struct avr_property_record *rec) 4228 1.5 christos { 4229 1.5 christos const char *str; 4230 1.5 christos 4231 1.5 christos switch (rec->type) 4232 1.5 christos { 4233 1.5 christos case RECORD_ORG: 4234 1.5 christos str = "ORG"; 4235 1.5 christos break; 4236 1.5 christos case RECORD_ORG_AND_FILL: 4237 1.5 christos str = "ORG+FILL"; 4238 1.5 christos break; 4239 1.5 christos case RECORD_ALIGN: 4240 1.5 christos str = "ALIGN"; 4241 1.5 christos break; 4242 1.5 christos case RECORD_ALIGN_AND_FILL: 4243 1.5 christos str = "ALIGN+FILL"; 4244 1.5 christos break; 4245 1.5 christos default: 4246 1.5 christos str = "unknown"; 4247 1.5 christos } 4248 1.5 christos 4249 1.5 christos return str; 4250 1.5 christos } 4251 1.5 christos 4252 1.1 christos 4253 1.1 christos #define ELF_ARCH bfd_arch_avr 4254 1.1 christos #define ELF_TARGET_ID AVR_ELF_DATA 4255 1.1 christos #define ELF_MACHINE_CODE EM_AVR 4256 1.1 christos #define ELF_MACHINE_ALT1 EM_AVR_OLD 4257 1.1 christos #define ELF_MAXPAGESIZE 1 4258 1.8 christos 4259 1.1 christos #define TARGET_LITTLE_SYM avr_elf32_vec 4260 1.1 christos #define TARGET_LITTLE_NAME "elf32-avr" 4261 1.1 christos 4262 1.1 christos #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create 4263 1.8 christos 4264 1.8 christos #define elf_info_to_howto avr_info_to_howto_rela 4265 1.8 christos #define elf_info_to_howto_rel NULL 4266 1.8 christos #define elf_backend_relocate_section elf32_avr_relocate_section 4267 1.1 christos #define elf_backend_can_gc_sections 1 4268 1.1 christos #define elf_backend_rela_normal 1 4269 1.1 christos #define elf_backend_final_write_processing \ 4270 1.1 christos bfd_elf_avr_final_write_processing 4271 1.1 christos #define elf_backend_object_p elf32_avr_object_p 4272 1.1 christos 4273 1.1 christos #define bfd_elf32_bfd_relax_section elf32_avr_relax_section 4274 1.8 christos #define bfd_elf32_bfd_get_relocated_section_contents \ 4275 1.5 christos elf32_avr_get_relocated_section_contents 4276 1.8 christos #define bfd_elf32_new_section_hook elf_avr_new_section_hook 4277 1.1 christos #define elf_backend_special_sections elf_avr_special_sections 4278 1.1 christos 4279 #include "elf32-target.h" 4280