Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-msp430.c
Line
Count
Source
1
/*  MSP430-specific support for 32-bit ELF
2
    Copyright (C) 2002-2026 Free Software Foundation, Inc.
3
    Contributed by Dmitry Diky <diwil@mail.ru>
4
5
    This file is part of BFD, the Binary File Descriptor library.
6
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
    MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "libiberty.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf/msp430.h"
28
29
static bool debug_relocs = 0;
30
31
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
32
0
#define OCTETS_PER_BYTE(ABFD, SEC) 1
33
34
static bfd_reloc_status_type
35
rl78_sym_diff_handler (bfd * abfd,
36
           arelent * reloc,
37
           asymbol * sym ATTRIBUTE_UNUSED,
38
           void * addr ATTRIBUTE_UNUSED,
39
           asection * input_sec,
40
           bfd * out_bfd ATTRIBUTE_UNUSED,
41
           char ** error_message ATTRIBUTE_UNUSED)
42
0
{
43
0
  bfd_size_type octets;
44
0
  octets = reloc->address * OCTETS_PER_BYTE (abfd, input_sec);
45
46
  /* Catch the case where bfd_install_relocation would return
47
     bfd_reloc_outofrange because the SYM_DIFF reloc is being used in a very
48
     small section.  It does not actually matter if this happens because all
49
     that SYM_DIFF does is compute a (4-byte) value.  A second reloc then uses
50
     this value, and it is that reloc that must fit into the section.
51
52
     This happens in eg, gcc/testsuite/gcc.c-torture/compile/labels-3.c.  */
53
0
  if ((octets + bfd_get_reloc_size (reloc->howto))
54
0
      > bfd_get_section_limit_octets (abfd, input_sec))
55
0
    return bfd_reloc_ok;
56
0
  return bfd_reloc_continue;
57
0
}
58
59
/* Special handler for relocations which don't have to be relocated.
60
   This function just simply returns bfd_reloc_ok.  */
61
static bfd_reloc_status_type
62
msp430_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
63
      asymbol *symbol ATTRIBUTE_UNUSED,
64
      void *data ATTRIBUTE_UNUSED, asection *input_section,
65
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
66
0
{
67
0
  if (output_bfd != NULL)
68
0
    reloc_entry->address += input_section->output_offset;
69
70
0
  return bfd_reloc_ok;
71
0
}
72
73
static reloc_howto_type elf_msp430_howto_table[] =
74
{
75
  HOWTO (R_MSP430_NONE,   /* type */
76
   0,     /* rightshift */
77
   0,     /* size */
78
   0,     /* bitsize */
79
   false,     /* pc_relative */
80
   0,     /* bitpos */
81
   complain_overflow_dont,/* complain_on_overflow */
82
   bfd_elf_generic_reloc, /* special_function */
83
   "R_MSP430_NONE", /* name */
84
   false,     /* partial_inplace */
85
   0,     /* src_mask */
86
   0,     /* dst_mask */
87
   false),    /* pcrel_offset */
88
89
  HOWTO (R_MSP430_32,   /* type */
90
   0,     /* rightshift */
91
   4,     /* size */
92
   32,      /* bitsize */
93
   false,     /* pc_relative */
94
   0,     /* bitpos */
95
   complain_overflow_bitfield,/* complain_on_overflow */
96
   bfd_elf_generic_reloc, /* special_function */
97
   "R_MSP430_32",   /* name */
98
   false,     /* partial_inplace */
99
   0xffffffff,    /* src_mask */
100
   0xffffffff,    /* dst_mask */
101
   false),    /* pcrel_offset */
102
103
  /* A 10 bit PC relative relocation.  */
104
  HOWTO (R_MSP430_10_PCREL, /* type */
105
   1,     /* rightshift */
106
   2,     /* size */
107
   10,      /* bitsize */
108
   true,      /* pc_relative */
109
   0,     /* bitpos */
110
   complain_overflow_bitfield,/* complain_on_overflow */
111
   bfd_elf_generic_reloc, /* special_function */
112
   "R_MSP430_10_PCREL", /* name */
113
   false,     /* partial_inplace */
114
   0x3ff,     /* src_mask */
115
   0x3ff,     /* dst_mask */
116
   true),     /* pcrel_offset */
117
118
  /* A 16 bit absolute relocation.  */
119
  HOWTO (R_MSP430_16,   /* type */
120
   0,     /* rightshift */
121
   2,     /* size */
122
   16,      /* bitsize */
123
   false,     /* pc_relative */
124
   0,     /* bitpos */
125
   complain_overflow_dont,/* complain_on_overflow */
126
   bfd_elf_generic_reloc, /* special_function */
127
   "R_MSP430_16",   /* name */
128
   false,     /* partial_inplace */
129
   0,     /* src_mask */
130
   0xffff,    /* dst_mask */
131
   false),    /* pcrel_offset */
132
133
  /* A 16 bit PC relative relocation for command address.  */
134
  HOWTO (R_MSP430_16_PCREL, /* type */
135
   1,     /* rightshift */
136
   2,     /* size */
137
   16,      /* bitsize */
138
   true,      /* pc_relative */
139
   0,     /* bitpos */
140
   complain_overflow_dont,/* complain_on_overflow */
141
   bfd_elf_generic_reloc, /* special_function */
142
   "R_MSP430_16_PCREL", /* name */
143
   false,     /* partial_inplace */
144
   0,     /* src_mask */
145
   0xffff,    /* dst_mask */
146
   true),     /* pcrel_offset */
147
148
  /* A 16 bit absolute relocation, byte operations.  */
149
  HOWTO (R_MSP430_16_BYTE,  /* type */
150
   0,     /* rightshift */
151
   2,     /* size */
152
   16,      /* bitsize */
153
   false,     /* pc_relative */
154
   0,     /* bitpos */
155
   complain_overflow_dont,/* complain_on_overflow */
156
   bfd_elf_generic_reloc, /* special_function */
157
   "R_MSP430_16_BYTE",  /* name */
158
   false,     /* partial_inplace */
159
   0xffff,    /* src_mask */
160
   0xffff,    /* dst_mask */
161
   false),    /* pcrel_offset */
162
163
  /* A 16 bit absolute relocation for command address.  */
164
  HOWTO (R_MSP430_16_PCREL_BYTE,/* type */
165
   1,     /* rightshift */
166
   2,     /* size */
167
   16,      /* bitsize */
168
   true,      /* pc_relative */
169
   0,     /* bitpos */
170
   complain_overflow_dont,/* complain_on_overflow */
171
   bfd_elf_generic_reloc, /* special_function */
172
   "R_MSP430_16_PCREL_BYTE",/* name */
173
   false,     /* partial_inplace */
174
   0xffff,    /* src_mask */
175
   0xffff,    /* dst_mask */
176
   true),     /* pcrel_offset */
177
178
  /* A 10 bit PC relative relocation for complicated polymorphs.  */
179
  HOWTO (R_MSP430_2X_PCREL, /* type */
180
   1,     /* rightshift */
181
   4,     /* size */
182
   10,      /* bitsize */
183
   true,      /* pc_relative */
184
   0,     /* bitpos */
185
   complain_overflow_bitfield,/* complain_on_overflow */
186
   bfd_elf_generic_reloc, /* special_function */
187
   "R_MSP430_2X_PCREL", /* name */
188
   false,     /* partial_inplace */
189
   0x3ff,     /* src_mask */
190
   0x3ff,     /* dst_mask */
191
   true),     /* pcrel_offset */
192
193
  /* A 16 bit relaxable relocation for command address.  */
194
  HOWTO (R_MSP430_RL_PCREL, /* type */
195
   1,     /* rightshift */
196
   2,     /* size */
197
   16,      /* bitsize */
198
   true,      /* pc_relative */
199
   0,     /* bitpos */
200
   complain_overflow_dont,/* complain_on_overflow */
201
   bfd_elf_generic_reloc, /* special_function */
202
   "R_MSP430_RL_PCREL", /* name */
203
   false,     /* partial_inplace */
204
   0,     /* src_mask */
205
   0xffff,    /* dst_mask */
206
   true)      /* pcrel_offset */
207
208
  /* A 8-bit absolute relocation.  */
209
  , HOWTO (R_MSP430_8,    /* type */
210
   0,     /* rightshift */
211
   1,     /* size */
212
   8,     /* bitsize */
213
   false,     /* pc_relative */
214
   0,     /* bitpos */
215
   complain_overflow_dont,/* complain_on_overflow */
216
   bfd_elf_generic_reloc, /* special_function */
217
   "R_MSP430_8",    /* name */
218
   false,     /* partial_inplace */
219
   0,     /* src_mask */
220
   0xffff,    /* dst_mask */
221
   false),    /* pcrel_offset */
222
223
  /* Together with a following reloc, allows for the difference
224
     between two symbols to be the real addend of the second reloc.  */
225
  HOWTO (R_MSP430_SYM_DIFF, /* type */
226
   0,     /* rightshift */
227
   4,     /* size */
228
   32,      /* bitsize */
229
   false,     /* pc_relative */
230
   0,     /* bitpos */
231
   complain_overflow_dont,/* complain_on_overflow */
232
   rl78_sym_diff_handler, /* special handler.  */
233
   "R_MSP430_SYM_DIFF", /* name */
234
   false,     /* partial_inplace */
235
   0xffffffff,    /* src_mask */
236
   0xffffffff,    /* dst_mask */
237
   false),    /* pcrel_offset */
238
239
  /* The length of unsigned-leb128 is variable, just assume the
240
     size is one byte here.  */
241
  HOWTO (R_MSP430_GNU_SET_ULEB128,  /* type */
242
   0,       /* rightshift */
243
   1,       /* size */
244
   0,       /* bitsize */
245
   false,       /* pc_relative */
246
   0,       /* bitpos */
247
   complain_overflow_dont,  /* complain_on_overflow */
248
   msp430_elf_ignore_reloc, /* special handler.  */
249
   "R_MSP430_GNU_SET_ULEB128",  /* name */
250
   false,       /* partial_inplace */
251
   0,       /* src_mask */
252
   0,       /* dst_mask */
253
   false),      /* pcrel_offset */
254
255
  /* The length of unsigned-leb128 is variable, just assume the
256
     size is one byte here.  */
257
  HOWTO (R_MSP430_GNU_SUB_ULEB128,  /* type */
258
   0,       /* rightshift */
259
   1,       /* size */
260
   0,       /* bitsize */
261
   false,       /* pc_relative */
262
   0,       /* bitpos */
263
   complain_overflow_dont,  /* complain_on_overflow */
264
   msp430_elf_ignore_reloc, /* special handler.  */
265
   "R_MSP430_GNU_SUB_ULEB128",  /* name */
266
   false,       /* partial_inplace */
267
   0,       /* src_mask */
268
   0,       /* dst_mask */
269
   false),      /* pcrel_offset */
270
271
};
272
273
static reloc_howto_type elf_msp430x_howto_table[] =
274
{
275
  HOWTO (R_MSP430_NONE,   /* type */
276
   0,     /* rightshift */
277
   0,     /* size */
278
   0,     /* bitsize */
279
   false,     /* pc_relative */
280
   0,     /* bitpos */
281
   complain_overflow_dont,/* complain_on_overflow */
282
   bfd_elf_generic_reloc, /* special_function */
283
   "R_MSP430_NONE", /* name */
284
   false,     /* partial_inplace */
285
   0,     /* src_mask */
286
   0,     /* dst_mask */
287
   false),    /* pcrel_offset */
288
289
  HOWTO (R_MSP430_ABS32,  /* type */
290
   0,     /* rightshift */
291
   4,     /* size */
292
   32,      /* bitsize */
293
   false,     /* pc_relative */
294
   0,     /* bitpos */
295
   complain_overflow_bitfield,/* complain_on_overflow */
296
   bfd_elf_generic_reloc, /* special_function */
297
   "R_MSP430_ABS32",  /* name */
298
   false,     /* partial_inplace */
299
   0xffffffff,    /* src_mask */
300
   0xffffffff,    /* dst_mask */
301
   false),    /* pcrel_offset */
302
303
  HOWTO (R_MSP430_ABS16,  /* type */
304
   0,     /* rightshift */
305
   2,     /* size */
306
   16,      /* bitsize */
307
   false,     /* pc_relative */
308
   0,     /* bitpos */
309
   complain_overflow_dont,/* complain_on_overflow */
310
   bfd_elf_generic_reloc, /* special_function */
311
   "R_MSP430_ABS16",  /* name */
312
   false,     /* partial_inplace */
313
   0,     /* src_mask */
314
   0xffff,    /* dst_mask */
315
   false),    /* pcrel_offset */
316
317
  HOWTO (R_MSP430_ABS8,   /* type */
318
   0,     /* rightshift */
319
   1,     /* size */
320
   8,     /* bitsize */
321
   false,     /* pc_relative */
322
   0,     /* bitpos */
323
   complain_overflow_bitfield,/* complain_on_overflow */
324
   bfd_elf_generic_reloc, /* special_function */
325
   "R_MSP430_ABS8", /* name */
326
   false,     /* partial_inplace */
327
   0xff,      /* src_mask */
328
   0xff,      /* dst_mask */
329
   false),    /* pcrel_offset */
330
331
  HOWTO (R_MSP430_PCR16,  /* type */
332
   1,     /* rightshift */
333
   2,     /* size */
334
   16,      /* bitsize */
335
   true,      /* pc_relative */
336
   0,     /* bitpos */
337
   complain_overflow_dont,/* complain_on_overflow */
338
   bfd_elf_generic_reloc, /* special_function */
339
   "R_MSP430_PCR16",  /* name */
340
   false,     /* partial_inplace */
341
   0,     /* src_mask */
342
   0xffff,    /* dst_mask */
343
   true),     /* pcrel_offset */
344
345
  HOWTO (R_MSP430X_PCR20_EXT_SRC,/* type */
346
   0,     /* rightshift */
347
   4,     /* size */
348
   32,      /* bitsize */
349
   true,      /* pc_relative */
350
   0,     /* bitpos */
351
   complain_overflow_dont,/* complain_on_overflow */
352
   bfd_elf_generic_reloc, /* special_function */
353
   "R_MSP430X_PCR20_EXT_SRC",/* name */
354
   false,     /* partial_inplace */
355
   0,     /* src_mask */
356
   0xffff,    /* dst_mask */
357
   true),     /* pcrel_offset */
358
359
  HOWTO (R_MSP430X_PCR20_EXT_DST,/* type */
360
   0,     /* rightshift */
361
   4,     /* size */
362
   32,      /* bitsize */
363
   true,      /* pc_relative */
364
   0,     /* bitpos */
365
   complain_overflow_dont,/* complain_on_overflow */
366
   bfd_elf_generic_reloc, /* special_function */
367
   "R_MSP430X_PCR20_EXT_DST",/* name */
368
   false,     /* partial_inplace */
369
   0,     /* src_mask */
370
   0xffff,    /* dst_mask */
371
   true),     /* pcrel_offset */
372
373
  HOWTO (R_MSP430X_PCR20_EXT_ODST,/* type */
374
   0,     /* rightshift */
375
   4,     /* size */
376
   32,      /* bitsize */
377
   true,      /* pc_relative */
378
   0,     /* bitpos */
379
   complain_overflow_dont,/* complain_on_overflow */
380
   bfd_elf_generic_reloc, /* special_function */
381
   "R_MSP430X_PCR20_EXT_ODST",/* name */
382
   false,     /* partial_inplace */
383
   0,     /* src_mask */
384
   0xffff,    /* dst_mask */
385
   true),     /* pcrel_offset */
386
387
  HOWTO (R_MSP430X_ABS20_EXT_SRC,/* type */
388
   0,     /* rightshift */
389
   4,     /* size */
390
   32,      /* bitsize */
391
   true,      /* pc_relative */
392
   0,     /* bitpos */
393
   complain_overflow_dont,/* complain_on_overflow */
394
   bfd_elf_generic_reloc, /* special_function */
395
   "R_MSP430X_ABS20_EXT_SRC",/* name */
396
   false,     /* partial_inplace */
397
   0,     /* src_mask */
398
   0xffff,    /* dst_mask */
399
   true),     /* pcrel_offset */
400
401
  HOWTO (R_MSP430X_ABS20_EXT_DST,/* type */
402
   0,     /* rightshift */
403
   4,     /* size */
404
   32,      /* bitsize */
405
   true,      /* pc_relative */
406
   0,     /* bitpos */
407
   complain_overflow_dont,/* complain_on_overflow */
408
   bfd_elf_generic_reloc, /* special_function */
409
   "R_MSP430X_ABS20_EXT_DST",/* name */
410
   false,     /* partial_inplace */
411
   0,     /* src_mask */
412
   0xffff,    /* dst_mask */
413
   true),     /* pcrel_offset */
414
415
  HOWTO (R_MSP430X_ABS20_EXT_ODST,/* type */
416
   0,     /* rightshift */
417
   4,     /* size */
418
   32,      /* bitsize */
419
   true,      /* pc_relative */
420
   0,     /* bitpos */
421
   complain_overflow_dont,/* complain_on_overflow */
422
   bfd_elf_generic_reloc, /* special_function */
423
   "R_MSP430X_ABS20_EXT_ODST",/* name */
424
   false,     /* partial_inplace */
425
   0,     /* src_mask */
426
   0xffff,    /* dst_mask */
427
   true),     /* pcrel_offset */
428
429
  HOWTO (R_MSP430X_ABS20_ADR_SRC,/* type */
430
   0,     /* rightshift */
431
   4,     /* size */
432
   32,      /* bitsize */
433
   true,      /* pc_relative */
434
   0,     /* bitpos */
435
   complain_overflow_dont,/* complain_on_overflow */
436
   bfd_elf_generic_reloc, /* special_function */
437
   "R_MSP430X_ABS20_ADR_SRC",/* name */
438
   false,     /* partial_inplace */
439
   0,     /* src_mask */
440
   0xffff,    /* dst_mask */
441
   true),     /* pcrel_offset */
442
443
  HOWTO (R_MSP430X_ABS20_ADR_DST,/* type */
444
   0,     /* rightshift */
445
   4,     /* size */
446
   32,      /* bitsize */
447
   true,      /* pc_relative */
448
   0,     /* bitpos */
449
   complain_overflow_dont,/* complain_on_overflow */
450
   bfd_elf_generic_reloc, /* special_function */
451
   "R_MSP430X_ABS20_ADR_DST",/* name */
452
   false,     /* partial_inplace */
453
   0,     /* src_mask */
454
   0xffff,    /* dst_mask */
455
   true),     /* pcrel_offset */
456
457
  HOWTO (R_MSP430X_PCR16, /* type */
458
   0,     /* rightshift */
459
   4,     /* size */
460
   32,      /* bitsize */
461
   true,      /* pc_relative */
462
   0,     /* bitpos */
463
   complain_overflow_dont,/* complain_on_overflow */
464
   bfd_elf_generic_reloc, /* special_function */
465
   "R_MSP430X_PCR16", /* name */
466
   false,     /* partial_inplace */
467
   0,     /* src_mask */
468
   0xffff,    /* dst_mask */
469
   true),     /* pcrel_offset */
470
471
  HOWTO (R_MSP430X_PCR20_CALL,  /* type */
472
   0,     /* rightshift */
473
   4,     /* size */
474
   32,      /* bitsize */
475
   true,      /* pc_relative */
476
   0,     /* bitpos */
477
   complain_overflow_dont,/* complain_on_overflow */
478
   bfd_elf_generic_reloc, /* special_function */
479
   "R_MSP430X_PCR20_CALL",/* name */
480
   false,     /* partial_inplace */
481
   0,     /* src_mask */
482
   0xffff,    /* dst_mask */
483
   true),     /* pcrel_offset */
484
485
  HOWTO (R_MSP430X_ABS16, /* type */
486
   0,     /* rightshift */
487
   4,     /* size */
488
   32,      /* bitsize */
489
   true,      /* pc_relative */
490
   0,     /* bitpos */
491
   complain_overflow_dont,/* complain_on_overflow */
492
   bfd_elf_generic_reloc, /* special_function */
493
   "R_MSP430X_ABS16", /* name */
494
   false,     /* partial_inplace */
495
   0,     /* src_mask */
496
   0xffff,    /* dst_mask */
497
   true),     /* pcrel_offset */
498
499
  HOWTO (R_MSP430_ABS_HI16, /* type */
500
   0,     /* rightshift */
501
   4,     /* size */
502
   32,      /* bitsize */
503
   true,      /* pc_relative */
504
   0,     /* bitpos */
505
   complain_overflow_dont,/* complain_on_overflow */
506
   bfd_elf_generic_reloc, /* special_function */
507
   "R_MSP430_ABS_HI16", /* name */
508
   false,     /* partial_inplace */
509
   0,     /* src_mask */
510
   0xffff,    /* dst_mask */
511
   true),     /* pcrel_offset */
512
513
  HOWTO (R_MSP430_PREL31, /* type */
514
   0,     /* rightshift */
515
   4,     /* size */
516
   32,      /* bitsize */
517
   true,      /* pc_relative */
518
   0,     /* bitpos */
519
   complain_overflow_dont,/* complain_on_overflow */
520
   bfd_elf_generic_reloc, /* special_function */
521
   "R_MSP430_PREL31", /* name */
522
   false,     /* partial_inplace */
523
   0,     /* src_mask */
524
   0xffff,    /* dst_mask */
525
   true),     /* pcrel_offset */
526
527
  EMPTY_HOWTO (R_MSP430_EHTYPE),
528
529
  /* A 10 bit PC relative relocation.  */
530
  HOWTO (R_MSP430X_10_PCREL,  /* type */
531
   1,     /* rightshift */
532
   2,     /* size */
533
   10,      /* bitsize */
534
   true,      /* pc_relative */
535
   0,     /* bitpos */
536
   complain_overflow_bitfield,/* complain_on_overflow */
537
   bfd_elf_generic_reloc, /* special_function */
538
   "R_MSP430X_10_PCREL",  /* name */
539
   false,     /* partial_inplace */
540
   0x3ff,     /* src_mask */
541
   0x3ff,     /* dst_mask */
542
   true),     /* pcrel_offset */
543
544
  /* A 10 bit PC relative relocation for complicated polymorphs.  */
545
  HOWTO (R_MSP430X_2X_PCREL,  /* type */
546
   1,     /* rightshift */
547
   4,     /* size */
548
   10,      /* bitsize */
549
   true,      /* pc_relative */
550
   0,     /* bitpos */
551
   complain_overflow_bitfield,/* complain_on_overflow */
552
   bfd_elf_generic_reloc, /* special_function */
553
   "R_MSP430X_2X_PCREL",  /* name */
554
   false,     /* partial_inplace */
555
   0x3ff,     /* src_mask */
556
   0x3ff,     /* dst_mask */
557
   true),     /* pcrel_offset */
558
559
  /* Together with a following reloc, allows for the difference
560
     between two symbols to be the real addend of the second reloc.  */
561
  HOWTO (R_MSP430X_SYM_DIFF,  /* type */
562
   0,     /* rightshift */
563
   4,     /* size */
564
   32,      /* bitsize */
565
   false,     /* pc_relative */
566
   0,     /* bitpos */
567
   complain_overflow_dont,/* complain_on_overflow */
568
   rl78_sym_diff_handler, /* special handler.  */
569
   "R_MSP430X_SYM_DIFF",  /* name */
570
   false,     /* partial_inplace */
571
   0xffffffff,    /* src_mask */
572
   0xffffffff,    /* dst_mask */
573
   false),    /* pcrel_offset */
574
575
  /* The length of unsigned-leb128 is variable, just assume the
576
     size is one byte here.  */
577
  HOWTO (R_MSP430X_GNU_SET_ULEB128, /* type */
578
   0,       /* rightshift */
579
   1,       /* size */
580
   0,       /* bitsize */
581
   false,       /* pc_relative */
582
   0,       /* bitpos */
583
   complain_overflow_dont,  /* complain_on_overflow */
584
   msp430_elf_ignore_reloc, /* special handler.  */
585
   "R_MSP430X_GNU_SET_ULEB128", /* name */
586
   false,       /* partial_inplace */
587
   0,       /* src_mask */
588
   0,       /* dst_mask */
589
   false),      /* pcrel_offset */
590
591
  /* The length of unsigned-leb128 is variable, just assume the
592
     size is one byte here.  */
593
  HOWTO (R_MSP430X_GNU_SUB_ULEB128, /* type */
594
   0,       /* rightshift */
595
   1,       /* size */
596
   0,       /* bitsize */
597
   false,       /* pc_relative */
598
   0,       /* bitpos */
599
   complain_overflow_dont,  /* complain_on_overflow */
600
   msp430_elf_ignore_reloc, /* special handler.  */
601
   "R_MSP430X_GNU_SUB_ULEB128", /* name */
602
   false,       /* partial_inplace */
603
   0,       /* src_mask */
604
   0,       /* dst_mask */
605
   false),      /* pcrel_offset */
606
607
};
608
609
/* Map BFD reloc types to MSP430 ELF reloc types.  */
610
611
struct msp430_reloc_map
612
{
613
  bfd_reloc_code_real_type bfd_reloc_val;
614
  unsigned int elf_reloc_val;
615
};
616
617
static const struct msp430_reloc_map msp430_reloc_map[] =
618
{
619
  {BFD_RELOC_NONE,       R_MSP430_NONE},
620
  {BFD_RELOC_32,       R_MSP430_32},
621
  {BFD_RELOC_MSP430_10_PCREL,    R_MSP430_10_PCREL},
622
  {BFD_RELOC_16,       R_MSP430_16_BYTE},
623
  {BFD_RELOC_MSP430_16_PCREL,    R_MSP430_16_PCREL},
624
  {BFD_RELOC_MSP430_16,      R_MSP430_16},
625
  {BFD_RELOC_MSP430_16_PCREL_BYTE, R_MSP430_16_PCREL_BYTE},
626
  {BFD_RELOC_MSP430_16_BYTE,     R_MSP430_16_BYTE},
627
  {BFD_RELOC_MSP430_2X_PCREL,    R_MSP430_2X_PCREL},
628
  {BFD_RELOC_MSP430_RL_PCREL,    R_MSP430_RL_PCREL},
629
  {BFD_RELOC_8,        R_MSP430_8},
630
  {BFD_RELOC_MSP430_SYM_DIFF,    R_MSP430_SYM_DIFF},
631
  {BFD_RELOC_MSP430_SET_ULEB128,   R_MSP430_GNU_SET_ULEB128 },
632
  {BFD_RELOC_MSP430_SUB_ULEB128,   R_MSP430_GNU_SUB_ULEB128 }
633
};
634
635
static const struct msp430_reloc_map msp430x_reloc_map[] =
636
{
637
  {BFD_RELOC_NONE,          R_MSP430_NONE},
638
  {BFD_RELOC_32,          R_MSP430_ABS32},
639
  {BFD_RELOC_16,          R_MSP430_ABS16},
640
  {BFD_RELOC_8,           R_MSP430_ABS8},
641
  {BFD_RELOC_MSP430_ABS8,       R_MSP430_ABS8},
642
  {BFD_RELOC_MSP430X_PCR20_EXT_SRC,   R_MSP430X_PCR20_EXT_SRC},
643
  {BFD_RELOC_MSP430X_PCR20_EXT_DST,   R_MSP430X_PCR20_EXT_DST},
644
  {BFD_RELOC_MSP430X_PCR20_EXT_ODST,  R_MSP430X_PCR20_EXT_ODST},
645
  {BFD_RELOC_MSP430X_ABS20_EXT_SRC,   R_MSP430X_ABS20_EXT_SRC},
646
  {BFD_RELOC_MSP430X_ABS20_EXT_DST,   R_MSP430X_ABS20_EXT_DST},
647
  {BFD_RELOC_MSP430X_ABS20_EXT_ODST,  R_MSP430X_ABS20_EXT_ODST},
648
  {BFD_RELOC_MSP430X_ABS20_ADR_SRC,   R_MSP430X_ABS20_ADR_SRC},
649
  {BFD_RELOC_MSP430X_ABS20_ADR_DST,   R_MSP430X_ABS20_ADR_DST},
650
  {BFD_RELOC_MSP430X_PCR16,       R_MSP430X_PCR16},
651
  {BFD_RELOC_MSP430X_PCR20_CALL,      R_MSP430X_PCR20_CALL},
652
  {BFD_RELOC_MSP430X_ABS16,       R_MSP430X_ABS16},
653
  {BFD_RELOC_MSP430_ABS_HI16,       R_MSP430_ABS_HI16},
654
  {BFD_RELOC_MSP430_PREL31,       R_MSP430_PREL31},
655
  {BFD_RELOC_MSP430_10_PCREL,       R_MSP430X_10_PCREL},
656
  {BFD_RELOC_MSP430_2X_PCREL,       R_MSP430X_2X_PCREL},
657
  {BFD_RELOC_MSP430_RL_PCREL,       R_MSP430X_PCR16},
658
  {BFD_RELOC_MSP430_SYM_DIFF,       R_MSP430X_SYM_DIFF},
659
  {BFD_RELOC_MSP430_SET_ULEB128,      R_MSP430X_GNU_SET_ULEB128 },
660
  {BFD_RELOC_MSP430_SUB_ULEB128,      R_MSP430X_GNU_SUB_ULEB128 }
661
};
662
663
static inline bool
664
uses_msp430x_relocs (bfd * abfd)
665
0
{
666
0
  extern const bfd_target msp430_elf32_ti_vec;
667
668
0
  return bfd_get_mach (abfd) == bfd_mach_msp430x
669
0
    || abfd->xvec == & msp430_elf32_ti_vec;
670
0
}
671
672
static reloc_howto_type *
673
bfd_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
674
         bfd_reloc_code_real_type code)
675
0
{
676
0
  unsigned int i;
677
678
0
  if (uses_msp430x_relocs (abfd))
679
0
    {
680
0
      for (i = ARRAY_SIZE (msp430x_reloc_map); i--;)
681
0
  if (msp430x_reloc_map[i].bfd_reloc_val == code)
682
0
    return elf_msp430x_howto_table + msp430x_reloc_map[i].elf_reloc_val;
683
0
    }
684
0
  else
685
0
    {
686
0
      for (i = 0; i < ARRAY_SIZE (msp430_reloc_map); i++)
687
0
  if (msp430_reloc_map[i].bfd_reloc_val == code)
688
0
    return &elf_msp430_howto_table[msp430_reloc_map[i].elf_reloc_val];
689
0
    }
690
691
0
  return NULL;
692
0
}
693
694
static reloc_howto_type *
695
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
696
         const char *r_name)
697
0
{
698
0
  unsigned int i;
699
700
0
  if (uses_msp430x_relocs (abfd))
701
0
    {
702
0
      for (i = ARRAY_SIZE (elf_msp430x_howto_table); i--;)
703
0
  if (elf_msp430x_howto_table[i].name != NULL
704
0
      && strcasecmp (elf_msp430x_howto_table[i].name, r_name) == 0)
705
0
    return elf_msp430x_howto_table + i;
706
0
    }
707
0
  else
708
0
    {
709
0
      for (i = 0;
710
0
     i < (sizeof (elf_msp430_howto_table)
711
0
    / sizeof (elf_msp430_howto_table[0]));
712
0
     i++)
713
0
  if (elf_msp430_howto_table[i].name != NULL
714
0
      && strcasecmp (elf_msp430_howto_table[i].name, r_name) == 0)
715
0
    return &elf_msp430_howto_table[i];
716
0
    }
717
718
0
  return NULL;
719
0
}
720
721
/* Set the howto pointer for an MSP430 ELF reloc.  */
722
723
static bool
724
msp430_info_to_howto_rela (bfd * abfd,
725
         arelent * cache_ptr,
726
         Elf_Internal_Rela * dst)
727
0
{
728
0
  unsigned int r_type;
729
730
0
  r_type = ELF32_R_TYPE (dst->r_info);
731
732
0
  if (uses_msp430x_relocs (abfd))
733
0
    {
734
0
      if (r_type >= (unsigned int) R_MSP430x_max)
735
0
  {
736
    /* xgettext:c-format */
737
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
738
0
            abfd, r_type);
739
0
    bfd_set_error (bfd_error_bad_value);
740
0
    return false;
741
0
  }
742
0
      cache_ptr->howto = elf_msp430x_howto_table + r_type;
743
0
    }
744
0
  else if (r_type >= (unsigned int) R_MSP430_max)
745
0
    {
746
      /* xgettext:c-format */
747
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
748
0
        abfd, r_type);
749
0
      bfd_set_error (bfd_error_bad_value);
750
0
      return false;
751
0
    }
752
0
  else
753
0
    cache_ptr->howto = &elf_msp430_howto_table[r_type];
754
755
0
  return true;
756
0
}
757
758
/* Look through the relocs for a section during the first phase.
759
   Since we don't do .gots or .plts, we just need to consider the
760
   virtual table relocs for gc.  */
761
762
static bool
763
elf32_msp430_check_relocs (bfd * abfd, struct bfd_link_info * info,
764
         asection * sec, const Elf_Internal_Rela * relocs)
765
0
{
766
0
  Elf_Internal_Shdr *symtab_hdr;
767
0
  struct elf_link_hash_entry **sym_hashes;
768
0
  const Elf_Internal_Rela *rel;
769
0
  const Elf_Internal_Rela *rel_end;
770
771
0
  if (bfd_link_relocatable (info))
772
0
    return true;
773
774
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
775
0
  sym_hashes = elf_sym_hashes (abfd);
776
777
0
  rel_end = relocs + sec->reloc_count;
778
0
  for (rel = relocs; rel < rel_end; rel++)
779
0
    {
780
0
      struct elf_link_hash_entry *h;
781
0
      unsigned long r_symndx;
782
783
0
      r_symndx = ELF32_R_SYM (rel->r_info);
784
0
      if (r_symndx < symtab_hdr->sh_info)
785
0
  h = NULL;
786
0
      else
787
0
  {
788
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
789
0
    while (h->root.type == bfd_link_hash_indirect
790
0
     || h->root.type == bfd_link_hash_warning)
791
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
792
0
  }
793
0
    }
794
795
0
  return true;
796
0
}
797
798
/* Perform a single relocation.  By default we use the standard BFD
799
   routines, but a few relocs, we have to do them ourselves.  */
800
801
static bfd_reloc_status_type
802
msp430_final_link_relocate (reloc_howto_type *     howto,
803
          bfd *      input_bfd,
804
          asection *       input_section,
805
          bfd_byte *       contents,
806
          Elf_Internal_Rela *    rel,
807
          bfd_vma      relocation,
808
          struct bfd_link_info * info)
809
0
{
810
0
  static asection *  sym_diff_section;
811
0
  static bfd_vma     sym_diff_value;
812
813
0
  struct bfd_elf_section_data * esd = elf_section_data (input_section);
814
0
  bfd_reloc_status_type r = bfd_reloc_ok;
815
0
  bfd_vma x;
816
0
  bfd_signed_vma srel;
817
0
  bool is_rel_reloc = false;
818
819
0
  if (uses_msp430x_relocs (input_bfd))
820
0
    {
821
      /* See if we have a REL type relocation.  */
822
0
      is_rel_reloc = (esd->rel.hdr != NULL);
823
      /* Sanity check - only one type of relocation per section.
824
   FIXME: Theoretically it is possible to have both types,
825
   but if that happens how can we distinguish between the two ?  */
826
0
      BFD_ASSERT (! is_rel_reloc || ! esd->rela.hdr);
827
      /* If we are using a REL relocation then the addend should be empty.  */
828
0
      BFD_ASSERT (! is_rel_reloc || rel->r_addend == 0);
829
0
    }
830
831
0
  if (debug_relocs)
832
0
    printf ("writing relocation (%p) at 0x%lx type: %d\n", rel,
833
0
      (long) (input_section->output_section->vma + input_section->output_offset
834
0
        + rel->r_offset), howto->type);
835
0
  if (sym_diff_section != NULL)
836
0
    {
837
0
      BFD_ASSERT (sym_diff_section == input_section);
838
839
0
     if (uses_msp430x_relocs (input_bfd))
840
0
       switch (howto->type)
841
0
   {
842
0
   case R_MSP430X_GNU_SET_ULEB128:
843
0
     relocation += (!is_rel_reloc ? rel->r_addend : 0);
844
     /* Fall through.  */
845
0
   case R_MSP430_ABS32:
846
    /* If we are computing a 32-bit value for the location lists
847
       and the result is 0 then we add one to the value.  A zero
848
       value can result because of linker relaxation deleteing
849
       prologue instructions and using a value of 1 (for the begin
850
       and end offsets in the location list entry) results in a
851
       nul entry which does not prevent the following entries from
852
       being parsed.  */
853
0
     if (relocation == sym_diff_value
854
0
         && strcmp (input_section->name, ".debug_loc") == 0)
855
0
       ++ relocation;
856
     /* Fall through.  */
857
0
   case R_MSP430_ABS16:
858
0
   case R_MSP430X_ABS16:
859
0
   case R_MSP430_ABS8:
860
0
     BFD_ASSERT (! is_rel_reloc);
861
0
     relocation -= sym_diff_value;
862
0
    break;
863
864
0
   default:
865
0
     return bfd_reloc_dangerous;
866
0
   }
867
0
     else
868
0
       switch (howto->type)
869
0
   {
870
0
   case R_MSP430_GNU_SET_ULEB128:
871
0
     relocation += (!is_rel_reloc ? rel->r_addend : 0);
872
     /* Fall through.  */
873
0
   case R_MSP430_32:
874
0
   case R_MSP430_16:
875
0
   case R_MSP430_16_BYTE:
876
0
   case R_MSP430_8:
877
0
     relocation -= sym_diff_value;
878
0
    break;
879
880
0
   default:
881
0
     return bfd_reloc_dangerous;
882
0
   }
883
884
0
      sym_diff_section = NULL;
885
0
    }
886
887
0
  if ((uses_msp430x_relocs (input_bfd)
888
0
       && howto->type == R_MSP430X_GNU_SET_ULEB128)
889
0
      || (!uses_msp430x_relocs (input_bfd)
890
0
    && howto->type == R_MSP430_GNU_SET_ULEB128))
891
0
    {
892
0
      unsigned int len, new_len = 0;
893
0
      bfd_byte *endp, *p;
894
0
      unsigned int val = relocation;
895
896
0
      p = contents + rel->r_offset;
897
0
      endp = contents + input_section->size;
898
0
      _bfd_safe_read_leb128 (input_bfd, &p, false, endp);
899
900
      /* Clean the contents value to zero.  Do not reduce the length.  */
901
0
      endp = p - 1;
902
0
      p = contents + rel->r_offset;
903
0
      len = endp + 1 - p;
904
0
      memset (p, 0x80, len - 1);
905
0
      *endp = 0;
906
907
      /* Get the length of the new uleb128 value.  */
908
0
      do
909
0
  {
910
0
    new_len++;
911
0
    val >>= 7;
912
0
  } while (val);
913
914
0
      if (new_len > len)
915
0
  {
916
0
    _bfd_error_handler
917
0
      (_("error: final size of uleb128 value at offset 0x%lx in %pA "
918
0
         "from %pB exceeds available space"),
919
0
       (long) rel->r_offset, input_section, input_bfd);
920
0
  }
921
0
      else
922
0
  {
923
    /* If the number of bytes required to store the new value has
924
       decreased, "right align" the new value within the available space,
925
       so the MSB side is padded with uleb128 zeros (0x80).  */
926
0
    p = _bfd_write_unsigned_leb128 (p + (len - new_len), endp,
927
0
            relocation);
928
    /* We checked there is enough space for the new value above, so this
929
       should never be NULL.  */
930
0
    BFD_ASSERT (p);
931
0
  }
932
933
0
      return bfd_reloc_ok;
934
0
    }
935
0
  else if (uses_msp430x_relocs (input_bfd))
936
0
    switch (howto->type)
937
0
      {
938
0
      case R_MSP430X_SYM_DIFF:
939
0
      case R_MSP430X_GNU_SUB_ULEB128:
940
  /* Cache the input section and value.
941
     The offset is unreliable, since relaxation may
942
     have reduced the following reloc's offset.  */
943
0
  BFD_ASSERT (! is_rel_reloc);
944
0
  sym_diff_section = input_section;
945
0
  sym_diff_value = relocation + (howto->type == R_MSP430X_GNU_SUB_ULEB128
946
0
               ? rel->r_addend : 0);
947
0
  return bfd_reloc_ok;
948
949
0
      case R_MSP430_ABS16:
950
0
  contents += rel->r_offset;
951
0
  srel = (bfd_signed_vma) relocation;
952
0
  if (is_rel_reloc)
953
0
    srel += bfd_get_16 (input_bfd, contents);
954
0
  else
955
0
    srel += rel->r_addend;
956
0
  bfd_put_16 (input_bfd, srel & 0xffff, contents);
957
0
  break;
958
959
0
      case R_MSP430X_10_PCREL:
960
0
  contents += rel->r_offset;
961
0
  srel = (bfd_signed_vma) relocation;
962
0
  if (is_rel_reloc)
963
0
    srel += bfd_get_16 (input_bfd, contents) & 0x3ff;
964
0
  else
965
0
    srel += rel->r_addend;
966
0
  srel -= rel->r_offset;
967
0
  srel -= 2;    /* Branch instructions add 2 to the PC...  */
968
0
  srel -= (input_section->output_section->vma +
969
0
     input_section->output_offset);
970
0
  if (srel & 1)
971
0
    return bfd_reloc_outofrange;
972
973
  /* MSP430 addresses commands as words.  */
974
0
  srel >>= 1;
975
976
  /* Check for an overflow.  */
977
0
  if (srel < -512 || srel > 511)
978
0
    {
979
0
      if (info->disable_target_specific_optimizations < 0)
980
0
        {
981
0
    static bool warned = false;
982
0
    if (! warned)
983
0
      {
984
0
        info->callbacks->warning
985
0
          (info,
986
0
           _("try enabling relaxation to avoid relocation truncations"),
987
0
           NULL, input_bfd, input_section, relocation);
988
0
        warned = true;
989
0
      }
990
0
        }
991
0
      return bfd_reloc_overflow;
992
0
    }
993
994
0
  x = bfd_get_16 (input_bfd, contents);
995
0
  x = (x & 0xfc00) | (srel & 0x3ff);
996
0
  bfd_put_16 (input_bfd, x, contents);
997
0
  break;
998
999
0
      case R_MSP430X_PCR20_EXT_ODST:
1000
  /* [0,4]+[48,16] = ---F ---- ---- FFFF */
1001
0
  contents += rel->r_offset;
1002
0
  srel = (bfd_signed_vma) relocation;
1003
0
  if (is_rel_reloc)
1004
0
    {
1005
0
      bfd_vma addend;
1006
0
      addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
1007
0
      addend |= bfd_get_16 (input_bfd, contents + 6);
1008
0
      srel += addend;
1009
1010
0
    }
1011
0
  else
1012
0
    srel += rel->r_addend;
1013
0
  srel -= rel->r_offset;
1014
0
  srel -= (input_section->output_section->vma +
1015
0
     input_section->output_offset);
1016
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
1017
0
  x = bfd_get_16 (input_bfd, contents);
1018
0
  x = (x & 0xfff0) | ((srel >> 16) & 0xf);
1019
0
  bfd_put_16 (input_bfd, x, contents);
1020
0
  break;
1021
1022
0
      case R_MSP430X_ABS20_EXT_SRC:
1023
  /* [7,4]+[32,16] = -78- ---- FFFF */
1024
0
  contents += rel->r_offset;
1025
0
  srel = (bfd_signed_vma) relocation;
1026
0
  if (is_rel_reloc)
1027
0
    {
1028
0
      bfd_vma addend;
1029
0
      addend = (bfd_get_16 (input_bfd, contents) & 0x0780) << 9;
1030
0
      addend |= bfd_get_16 (input_bfd, contents + 4);
1031
0
      srel += addend;
1032
0
    }
1033
0
  else
1034
0
    srel += rel->r_addend;
1035
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
1036
0
  srel >>= 16;
1037
0
  x = bfd_get_16 (input_bfd, contents);
1038
0
  x = (x & 0xf87f) | ((srel << 7) & 0x0780);
1039
0
  bfd_put_16 (input_bfd, x, contents);
1040
0
  break;
1041
1042
0
      case R_MSP430_16_PCREL:
1043
0
  contents += rel->r_offset;
1044
0
  srel = (bfd_signed_vma) relocation;
1045
0
  if (is_rel_reloc)
1046
0
    srel += bfd_get_16 (input_bfd, contents);
1047
0
  else
1048
0
    srel += rel->r_addend;
1049
0
  srel -= rel->r_offset;
1050
  /* Only branch instructions add 2 to the PC...  */
1051
0
  srel -= (input_section->output_section->vma +
1052
0
     input_section->output_offset);
1053
0
  if (srel & 1)
1054
0
    return bfd_reloc_outofrange;
1055
0
  bfd_put_16 (input_bfd, srel & 0xffff, contents);
1056
0
  break;
1057
1058
0
      case R_MSP430X_PCR20_EXT_DST:
1059
  /* [0,4]+[32,16] = ---F ---- FFFF */
1060
0
  contents += rel->r_offset;
1061
0
  srel = (bfd_signed_vma) relocation;
1062
0
  if (is_rel_reloc)
1063
0
    {
1064
0
      bfd_vma addend;
1065
0
      addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
1066
0
      addend |= bfd_get_16 (input_bfd, contents + 4);
1067
0
      srel += addend;
1068
0
    }
1069
0
  else
1070
0
    srel += rel->r_addend;
1071
0
  srel -= rel->r_offset;
1072
0
  srel -= (input_section->output_section->vma +
1073
0
     input_section->output_offset);
1074
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
1075
0
  srel >>= 16;
1076
0
  x = bfd_get_16 (input_bfd, contents);
1077
0
  x = (x & 0xfff0) | (srel & 0xf);
1078
0
  bfd_put_16 (input_bfd, x, contents);
1079
0
  break;
1080
1081
0
      case R_MSP430X_PCR20_EXT_SRC:
1082
  /* [7,4]+[32,16] = -78- ---- FFFF */
1083
0
  contents += rel->r_offset;
1084
0
  srel = (bfd_signed_vma) relocation;
1085
0
  if (is_rel_reloc)
1086
0
    {
1087
0
      bfd_vma addend;
1088
0
      addend = ((bfd_get_16 (input_bfd, contents) & 0x0780) << 9);
1089
0
      addend |= bfd_get_16 (input_bfd, contents + 4);
1090
0
      srel += addend;;
1091
0
    }
1092
0
  else
1093
0
    srel += rel->r_addend;
1094
0
  srel -= rel->r_offset;
1095
  /* Only branch instructions add 2 to the PC...  */
1096
0
  srel -= (input_section->output_section->vma +
1097
0
     input_section->output_offset);
1098
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
1099
0
  srel >>= 16;
1100
0
  x = bfd_get_16 (input_bfd, contents);
1101
0
  x = (x & 0xf87f) | ((srel << 7) & 0x0780);
1102
0
  bfd_put_16 (input_bfd, x, contents);
1103
0
  break;
1104
1105
0
      case R_MSP430_ABS8:
1106
0
  contents += rel->r_offset;
1107
0
  srel = (bfd_signed_vma) relocation;
1108
0
  if (is_rel_reloc)
1109
0
    srel += bfd_get_8 (input_bfd, contents);
1110
0
  else
1111
0
    srel += rel->r_addend;
1112
0
  bfd_put_8 (input_bfd, srel & 0xff, contents);
1113
0
  break;
1114
1115
0
      case R_MSP430X_ABS20_EXT_DST:
1116
  /* [0,4]+[32,16] = ---F ---- FFFF */
1117
0
  contents += rel->r_offset;
1118
0
  srel = (bfd_signed_vma) relocation;
1119
0
  if (is_rel_reloc)
1120
0
    {
1121
0
      bfd_vma addend;
1122
0
      addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
1123
0
      addend |= bfd_get_16 (input_bfd, contents + 4);
1124
0
      srel += addend;
1125
0
    }
1126
0
  else
1127
0
    srel += rel->r_addend;
1128
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
1129
0
  srel >>= 16;
1130
0
  x = bfd_get_16 (input_bfd, contents);
1131
0
  x = (x & 0xfff0) | (srel & 0xf);
1132
0
  bfd_put_16 (input_bfd, x, contents);
1133
0
  break;
1134
1135
0
      case R_MSP430X_ABS20_EXT_ODST:
1136
  /* [0,4]+[48,16] = ---F ---- ---- FFFF */
1137
0
  contents += rel->r_offset;
1138
0
  srel = (bfd_signed_vma) relocation;
1139
0
  if (is_rel_reloc)
1140
0
    {
1141
0
      bfd_vma addend;
1142
0
      addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
1143
0
      addend |= bfd_get_16 (input_bfd, contents + 6);
1144
0
      srel += addend;
1145
0
    }
1146
0
  else
1147
0
    srel += rel->r_addend;
1148
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
1149
0
  srel >>= 16;
1150
0
  x = bfd_get_16 (input_bfd, contents);
1151
0
  x = (x & 0xfff0) | (srel & 0xf);
1152
0
  bfd_put_16 (input_bfd, x, contents);
1153
0
  break;
1154
1155
0
      case R_MSP430X_ABS20_ADR_SRC:
1156
  /* [8,4]+[16,16] = -F-- FFFF */
1157
0
  contents += rel->r_offset;
1158
0
  srel = (bfd_signed_vma) relocation;
1159
0
  if (is_rel_reloc)
1160
0
    {
1161
0
      bfd_vma addend;
1162
1163
0
      addend = ((bfd_get_16 (input_bfd, contents) & 0xf00) << 8);
1164
0
      addend |= bfd_get_16 (input_bfd, contents + 2);
1165
0
      srel += addend;
1166
0
    }
1167
0
  else
1168
0
    srel += rel->r_addend;
1169
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
1170
0
  srel >>= 16;
1171
0
  x = bfd_get_16 (input_bfd, contents);
1172
0
  x = (x & 0xf0ff) | ((srel << 8) & 0x0f00);
1173
0
  bfd_put_16 (input_bfd, x, contents);
1174
0
  break;
1175
1176
0
      case R_MSP430X_ABS20_ADR_DST:
1177
  /* [0,4]+[16,16] = ---F FFFF */
1178
0
  contents += rel->r_offset;
1179
0
  srel = (bfd_signed_vma) relocation;
1180
0
  if (is_rel_reloc)
1181
0
    {
1182
0
      bfd_vma addend;
1183
0
      addend = ((bfd_get_16 (input_bfd, contents) & 0xf) << 16);
1184
0
      addend |= bfd_get_16 (input_bfd, contents + 2);
1185
0
      srel += addend;
1186
0
    }
1187
0
  else
1188
0
    srel += rel->r_addend;
1189
0
  bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
1190
0
  srel >>= 16;
1191
0
  x = bfd_get_16 (input_bfd, contents);
1192
0
  x = (x & 0xfff0) | (srel & 0xf);
1193
0
  bfd_put_16 (input_bfd, x, contents);
1194
0
  break;
1195
1196
0
      case R_MSP430X_ABS16:
1197
0
  contents += rel->r_offset;
1198
0
  srel = (bfd_signed_vma) relocation;
1199
0
  if (is_rel_reloc)
1200
0
    srel += bfd_get_16 (input_bfd, contents);
1201
0
  else
1202
0
    srel += rel->r_addend;
1203
0
  x = srel;
1204
0
  if (x > 0xffff)
1205
0
    return bfd_reloc_overflow;
1206
0
  bfd_put_16 (input_bfd, srel & 0xffff, contents);
1207
0
  break;
1208
1209
0
      case R_MSP430_ABS_HI16:
1210
  /* The EABI specifies that this must be a RELA reloc.  */
1211
0
  BFD_ASSERT (! is_rel_reloc);
1212
0
  contents += rel->r_offset;
1213
0
  srel = (bfd_signed_vma) relocation;
1214
0
  srel += rel->r_addend;
1215
0
  bfd_put_16 (input_bfd, (srel >> 16) & 0xffff, contents);
1216
0
  break;
1217
1218
0
      case R_MSP430X_PCR20_CALL:
1219
  /* [0,4]+[16,16] = ---F FFFF*/
1220
0
  contents += rel->r_offset;
1221
0
  srel = (bfd_signed_vma) relocation;
1222
0
  if (is_rel_reloc)
1223
0
    {
1224
0
      bfd_vma addend;
1225
0
      addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
1226
0
      addend |= bfd_get_16 (input_bfd, contents + 2);
1227
0
      srel += addend;
1228
0
    }
1229
0
  else
1230
0
    srel += rel->r_addend;
1231
0
  srel -= rel->r_offset;
1232
0
  srel -= (input_section->output_section->vma +
1233
0
     input_section->output_offset);
1234
0
  bfd_put_16 (input_bfd, srel & 0xffff, contents + 2);
1235
0
  srel >>= 16;
1236
0
  x = bfd_get_16 (input_bfd, contents);
1237
0
  x = (x & 0xfff0) | (srel & 0xf);
1238
0
  bfd_put_16 (input_bfd, x, contents);
1239
0
  break;
1240
1241
0
      case R_MSP430X_PCR16:
1242
0
  contents += rel->r_offset;
1243
0
  srel = (bfd_signed_vma) relocation;
1244
0
  if (is_rel_reloc)
1245
0
    srel += bfd_get_16 (input_bfd, contents);
1246
0
  else
1247
0
    srel += rel->r_addend;
1248
0
  srel -= rel->r_offset;
1249
0
  srel -= (input_section->output_section->vma +
1250
0
     input_section->output_offset);
1251
0
  bfd_put_16 (input_bfd, srel & 0xffff, contents);
1252
0
  break;
1253
1254
0
      case R_MSP430_PREL31:
1255
0
  contents += rel->r_offset;
1256
0
  srel = (bfd_signed_vma) relocation;
1257
0
  if (is_rel_reloc)
1258
0
    srel += (bfd_get_32 (input_bfd, contents) & 0x7fffffff);
1259
0
  else
1260
0
    srel += rel->r_addend;
1261
0
  srel += rel->r_addend;
1262
0
  x = bfd_get_32 (input_bfd, contents);
1263
0
  x = (x & 0x80000000) | ((srel >> 31) & 0x7fffffff);
1264
0
  bfd_put_32 (input_bfd, x, contents);
1265
0
  break;
1266
1267
0
      default:
1268
0
  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1269
0
              contents, rel->r_offset,
1270
0
              relocation, rel->r_addend);
1271
0
      }
1272
0
  else
1273
0
    switch (howto->type)
1274
0
      {
1275
0
    case R_MSP430_10_PCREL:
1276
0
      contents += rel->r_offset;
1277
0
      srel = (bfd_signed_vma) relocation;
1278
0
      srel += rel->r_addend;
1279
0
      srel -= rel->r_offset;
1280
0
      srel -= 2;    /* Branch instructions add 2 to the PC...  */
1281
0
      srel -= (input_section->output_section->vma +
1282
0
         input_section->output_offset);
1283
1284
0
      if (srel & 1)
1285
0
  return bfd_reloc_outofrange;
1286
1287
      /* MSP430 addresses commands as words.  */
1288
0
      srel >>= 1;
1289
1290
      /* Check for an overflow.  */
1291
0
      if (srel < -512 || srel > 511)
1292
0
  {
1293
0
    if (info->disable_target_specific_optimizations < 0)
1294
0
      {
1295
0
        static bool warned = false;
1296
0
        if (! warned)
1297
0
    {
1298
0
      info->callbacks->warning
1299
0
        (info,
1300
0
         _("try enabling relaxation to avoid relocation truncations"),
1301
0
         NULL, input_bfd, input_section, relocation);
1302
0
      warned = true;
1303
0
    }
1304
0
      }
1305
0
    return bfd_reloc_overflow;
1306
0
  }
1307
1308
0
      x = bfd_get_16 (input_bfd, contents);
1309
0
      x = (x & 0xfc00) | (srel & 0x3ff);
1310
0
      bfd_put_16 (input_bfd, x, contents);
1311
0
      break;
1312
1313
0
    case R_MSP430_2X_PCREL:
1314
0
      contents += rel->r_offset;
1315
0
      srel = (bfd_signed_vma) relocation;
1316
0
      srel += rel->r_addend;
1317
0
      srel -= rel->r_offset;
1318
0
      srel -= 2;    /* Branch instructions add 2 to the PC...  */
1319
0
      srel -= (input_section->output_section->vma +
1320
0
         input_section->output_offset);
1321
1322
0
      if (srel & 1)
1323
0
  return bfd_reloc_outofrange;
1324
1325
      /* MSP430 addresses commands as words.  */
1326
0
      srel >>= 1;
1327
1328
      /* Check for an overflow.  */
1329
0
      if (srel < -512 || srel > 511)
1330
0
  return bfd_reloc_overflow;
1331
1332
0
      x = bfd_get_16 (input_bfd, contents);
1333
0
      x = (x & 0xfc00) | (srel & 0x3ff);
1334
0
      bfd_put_16 (input_bfd, x, contents);
1335
      /* Handle second jump instruction.  */
1336
0
      x = bfd_get_16 (input_bfd, contents - 2);
1337
0
      srel += 1;
1338
0
      x = (x & 0xfc00) | (srel & 0x3ff);
1339
0
      bfd_put_16 (input_bfd, x, contents - 2);
1340
0
      break;
1341
1342
0
    case R_MSP430_RL_PCREL:
1343
0
    case R_MSP430_16_PCREL:
1344
0
      contents += rel->r_offset;
1345
0
      srel = (bfd_signed_vma) relocation;
1346
0
      srel += rel->r_addend;
1347
0
      srel -= rel->r_offset;
1348
      /* Only branch instructions add 2 to the PC...  */
1349
0
      srel -= (input_section->output_section->vma +
1350
0
         input_section->output_offset);
1351
1352
0
      if (srel & 1)
1353
0
  return bfd_reloc_outofrange;
1354
1355
0
      bfd_put_16 (input_bfd, srel & 0xffff, contents);
1356
0
      break;
1357
1358
0
    case R_MSP430_16_PCREL_BYTE:
1359
0
      contents += rel->r_offset;
1360
0
      srel = (bfd_signed_vma) relocation;
1361
0
      srel += rel->r_addend;
1362
0
      srel -= rel->r_offset;
1363
      /* Only branch instructions add 2 to the PC...  */
1364
0
      srel -= (input_section->output_section->vma +
1365
0
         input_section->output_offset);
1366
1367
0
      bfd_put_16 (input_bfd, srel & 0xffff, contents);
1368
0
      break;
1369
1370
0
    case R_MSP430_16_BYTE:
1371
0
      contents += rel->r_offset;
1372
0
      srel = (bfd_signed_vma) relocation;
1373
0
      srel += rel->r_addend;
1374
0
      bfd_put_16 (input_bfd, srel & 0xffff, contents);
1375
0
      break;
1376
1377
0
    case R_MSP430_16:
1378
0
      contents += rel->r_offset;
1379
0
      srel = (bfd_signed_vma) relocation;
1380
0
      srel += rel->r_addend;
1381
1382
0
      if (srel & 1)
1383
0
  return bfd_reloc_notsupported;
1384
1385
0
      bfd_put_16 (input_bfd, srel & 0xffff, contents);
1386
0
      break;
1387
1388
0
    case R_MSP430_8:
1389
0
      contents += rel->r_offset;
1390
0
      srel = (bfd_signed_vma) relocation;
1391
0
      srel += rel->r_addend;
1392
1393
0
      bfd_put_8 (input_bfd, srel & 0xff, contents);
1394
0
      break;
1395
1396
0
    case R_MSP430_SYM_DIFF:
1397
0
    case R_MSP430_GNU_SUB_ULEB128:
1398
      /* Cache the input section and value.
1399
   The offset is unreliable, since relaxation may
1400
   have reduced the following reloc's offset.  */
1401
0
      sym_diff_section = input_section;
1402
0
      sym_diff_value = relocation + (howto->type == R_MSP430_GNU_SUB_ULEB128
1403
0
             ? rel->r_addend : 0);
1404
0
      return bfd_reloc_ok;
1405
1406
0
      default:
1407
0
  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1408
0
              contents, rel->r_offset,
1409
0
              relocation, rel->r_addend);
1410
0
      }
1411
1412
0
  return r;
1413
0
}
1414
1415
/* Relocate an MSP430 ELF section.  */
1416
1417
static int
1418
elf32_msp430_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
1419
             struct bfd_link_info * info,
1420
             bfd * input_bfd,
1421
             asection * input_section,
1422
             bfd_byte * contents,
1423
             Elf_Internal_Rela * relocs,
1424
             Elf_Internal_Sym * local_syms,
1425
             asection ** local_sections)
1426
0
{
1427
0
  Elf_Internal_Shdr *symtab_hdr;
1428
0
  struct elf_link_hash_entry **sym_hashes;
1429
0
  Elf_Internal_Rela *rel;
1430
0
  Elf_Internal_Rela *relend;
1431
1432
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1433
0
  sym_hashes = elf_sym_hashes (input_bfd);
1434
0
  relend = relocs + input_section->reloc_count;
1435
1436
0
  for (rel = relocs; rel < relend; rel++)
1437
0
    {
1438
0
      reloc_howto_type *howto;
1439
0
      unsigned long r_symndx;
1440
0
      Elf_Internal_Sym *sym;
1441
0
      asection *sec;
1442
0
      struct elf_link_hash_entry *h;
1443
0
      bfd_vma relocation;
1444
0
      bfd_reloc_status_type r;
1445
0
      const char *name = NULL;
1446
0
      int r_type;
1447
1448
0
      r_type = ELF32_R_TYPE (rel->r_info);
1449
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1450
1451
0
      if (uses_msp430x_relocs (input_bfd))
1452
0
  howto = elf_msp430x_howto_table + r_type;
1453
0
      else
1454
0
  howto = elf_msp430_howto_table + r_type;
1455
1456
0
      h = NULL;
1457
0
      sym = NULL;
1458
0
      sec = NULL;
1459
1460
0
      if (r_symndx < symtab_hdr->sh_info)
1461
0
  {
1462
0
    sym = local_syms + r_symndx;
1463
0
    sec = local_sections[r_symndx];
1464
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1465
1466
0
    name = bfd_elf_string_from_elf_section
1467
0
        (input_bfd, symtab_hdr->sh_link, sym->st_name);
1468
0
    name = name == NULL || *name == 0 ? bfd_section_name (sec) : name;
1469
0
  }
1470
0
      else
1471
0
  {
1472
0
    bool unresolved_reloc, warned, ignored;
1473
1474
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1475
0
           r_symndx, symtab_hdr, sym_hashes,
1476
0
           h, sec, relocation,
1477
0
           unresolved_reloc, warned, ignored);
1478
0
    name = h->root.root.string;
1479
0
  }
1480
1481
0
      if (sec != NULL && discarded_section (sec))
1482
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1483
0
           rel, 1, relend, R_MSP430_NONE,
1484
0
           howto, 0, contents);
1485
1486
0
      if (bfd_link_relocatable (info))
1487
0
  continue;
1488
1489
0
      r = msp430_final_link_relocate (howto, input_bfd, input_section,
1490
0
              contents, rel, relocation, info);
1491
1492
0
      if (r != bfd_reloc_ok)
1493
0
  {
1494
0
    const char *msg = (const char *) NULL;
1495
1496
0
    switch (r)
1497
0
      {
1498
0
      case bfd_reloc_overflow:
1499
0
        (*info->callbacks->reloc_overflow)
1500
0
    (info, (h ? &h->root : NULL), name, howto->name,
1501
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1502
0
        break;
1503
1504
0
      case bfd_reloc_undefined:
1505
0
        (*info->callbacks->undefined_symbol)
1506
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
1507
0
        break;
1508
1509
0
      case bfd_reloc_outofrange:
1510
0
        msg = _("internal error: branch/jump to an odd address detected");
1511
0
        break;
1512
1513
0
      case bfd_reloc_notsupported:
1514
0
        msg = _("internal error: unsupported relocation error");
1515
0
        break;
1516
1517
0
      case bfd_reloc_dangerous:
1518
0
        msg = _("internal error: dangerous relocation");
1519
0
        break;
1520
1521
0
      default:
1522
0
        msg = _("internal error: unknown error");
1523
0
        break;
1524
0
      }
1525
1526
0
    if (msg)
1527
0
      (*info->callbacks->warning) (info, msg, name, input_bfd,
1528
0
           input_section, rel->r_offset);
1529
0
  }
1530
1531
0
    }
1532
1533
0
  return true;
1534
0
}
1535
1536
/* The final processing done just before writing out a MSP430 ELF object
1537
   file.  This gets the MSP430 architecture right based on the machine
1538
   number.  */
1539
1540
static bool
1541
bfd_elf_msp430_final_write_processing (bfd *abfd)
1542
0
{
1543
0
  unsigned long val;
1544
1545
0
  switch (bfd_get_mach (abfd))
1546
0
    {
1547
0
    default:
1548
0
    case bfd_mach_msp110: val = E_MSP430_MACH_MSP430x11x1; break;
1549
0
    case bfd_mach_msp11: val = E_MSP430_MACH_MSP430x11; break;
1550
0
    case bfd_mach_msp12: val = E_MSP430_MACH_MSP430x12; break;
1551
0
    case bfd_mach_msp13: val = E_MSP430_MACH_MSP430x13; break;
1552
0
    case bfd_mach_msp14: val = E_MSP430_MACH_MSP430x14; break;
1553
0
    case bfd_mach_msp15: val = E_MSP430_MACH_MSP430x15; break;
1554
0
    case bfd_mach_msp16: val = E_MSP430_MACH_MSP430x16; break;
1555
0
    case bfd_mach_msp31: val = E_MSP430_MACH_MSP430x31; break;
1556
0
    case bfd_mach_msp32: val = E_MSP430_MACH_MSP430x32; break;
1557
0
    case bfd_mach_msp33: val = E_MSP430_MACH_MSP430x33; break;
1558
0
    case bfd_mach_msp41: val = E_MSP430_MACH_MSP430x41; break;
1559
0
    case bfd_mach_msp42: val = E_MSP430_MACH_MSP430x42; break;
1560
0
    case bfd_mach_msp43: val = E_MSP430_MACH_MSP430x43; break;
1561
0
    case bfd_mach_msp44: val = E_MSP430_MACH_MSP430x44; break;
1562
0
    case bfd_mach_msp20: val = E_MSP430_MACH_MSP430x20; break;
1563
0
    case bfd_mach_msp22: val = E_MSP430_MACH_MSP430x22; break;
1564
0
    case bfd_mach_msp23: val = E_MSP430_MACH_MSP430x23; break;
1565
0
    case bfd_mach_msp24: val = E_MSP430_MACH_MSP430x24; break;
1566
0
    case bfd_mach_msp26: val = E_MSP430_MACH_MSP430x26; break;
1567
0
    case bfd_mach_msp46: val = E_MSP430_MACH_MSP430x46; break;
1568
0
    case bfd_mach_msp47: val = E_MSP430_MACH_MSP430x47; break;
1569
0
    case bfd_mach_msp54: val = E_MSP430_MACH_MSP430x54; break;
1570
0
    case bfd_mach_msp430x: val = E_MSP430_MACH_MSP430X; break;
1571
0
    }
1572
1573
0
  elf_elfheader (abfd)->e_machine = EM_MSP430;
1574
0
  elf_elfheader (abfd)->e_flags &= ~EF_MSP430_MACH;
1575
0
  elf_elfheader (abfd)->e_flags |= val;
1576
0
  return _bfd_elf_final_write_processing (abfd);
1577
0
}
1578
1579
/* Set the right machine number.  */
1580
1581
static bool
1582
elf32_msp430_object_p (bfd * abfd)
1583
238
{
1584
238
  int e_set = bfd_mach_msp14;
1585
1586
238
  if (elf_elfheader (abfd)->e_machine == EM_MSP430
1587
102
      || elf_elfheader (abfd)->e_machine == EM_MSP430_OLD)
1588
238
    {
1589
238
      int e_mach = elf_elfheader (abfd)->e_flags & EF_MSP430_MACH;
1590
1591
238
      switch (e_mach)
1592
238
  {
1593
28
  default:
1594
28
  case E_MSP430_MACH_MSP430x11: e_set = bfd_mach_msp11; break;
1595
5
  case E_MSP430_MACH_MSP430x11x1: e_set = bfd_mach_msp110; break;
1596
10
  case E_MSP430_MACH_MSP430x12: e_set = bfd_mach_msp12; break;
1597
7
  case E_MSP430_MACH_MSP430x13: e_set = bfd_mach_msp13; break;
1598
10
  case E_MSP430_MACH_MSP430x14: e_set = bfd_mach_msp14; break;
1599
10
  case E_MSP430_MACH_MSP430x15: e_set = bfd_mach_msp15; break;
1600
10
  case E_MSP430_MACH_MSP430x16: e_set = bfd_mach_msp16; break;
1601
10
  case E_MSP430_MACH_MSP430x31: e_set = bfd_mach_msp31; break;
1602
12
  case E_MSP430_MACH_MSP430x32: e_set = bfd_mach_msp32; break;
1603
13
  case E_MSP430_MACH_MSP430x33: e_set = bfd_mach_msp33; break;
1604
10
  case E_MSP430_MACH_MSP430x41: e_set = bfd_mach_msp41; break;
1605
8
  case E_MSP430_MACH_MSP430x42: e_set = bfd_mach_msp42; break;
1606
10
  case E_MSP430_MACH_MSP430x43: e_set = bfd_mach_msp43; break;
1607
8
  case E_MSP430_MACH_MSP430x44: e_set = bfd_mach_msp44; break;
1608
10
  case E_MSP430_MACH_MSP430x20: e_set = bfd_mach_msp20; break;
1609
10
  case E_MSP430_MACH_MSP430x22: e_set = bfd_mach_msp22; break;
1610
10
  case E_MSP430_MACH_MSP430x23: e_set = bfd_mach_msp23; break;
1611
10
  case E_MSP430_MACH_MSP430x24: e_set = bfd_mach_msp24; break;
1612
7
  case E_MSP430_MACH_MSP430x26: e_set = bfd_mach_msp26; break;
1613
10
  case E_MSP430_MACH_MSP430x46: e_set = bfd_mach_msp46; break;
1614
10
  case E_MSP430_MACH_MSP430x47: e_set = bfd_mach_msp47; break;
1615
10
  case E_MSP430_MACH_MSP430x54: e_set = bfd_mach_msp54; break;
1616
10
  case E_MSP430_MACH_MSP430X: e_set = bfd_mach_msp430x; break;
1617
238
  }
1618
238
    }
1619
1620
238
  return bfd_default_set_arch_mach (abfd, bfd_arch_msp430, e_set);
1621
238
}
1622
1623
/* These functions handle relaxing for the msp430.
1624
   Relaxation required only in two cases:
1625
    - Bad hand coding like jumps from one section to another or
1626
      from file to file.
1627
    - Sibling calls. This will affect only 'jump label' polymorph. Without
1628
      relaxing this enlarges code by 2 bytes. Sibcalls implemented but
1629
      do not work in gcc's port by the reason I do not know.
1630
    - To convert out of range conditional jump instructions (found inside
1631
      a function) into inverted jumps over an unconditional branch instruction.
1632
   Anyway, if a relaxation required, user should pass -relax option to the
1633
   linker.
1634
1635
   There are quite a few relaxing opportunities available on the msp430:
1636
1637
   ================================================================
1638
1639
   1. 3 words -> 1 word
1640
1641
   eq    ==    jeq label      jne +4; br lab
1642
   ne    !=    jne label      jeq +4; br lab
1643
   lt    <     jl  label      jge +4; br lab
1644
   ltu     <     jlo label      lhs +4; br lab
1645
   ge    >=    jge label      jl  +4; br lab
1646
   geu     >=    jhs label      jlo +4; br lab
1647
1648
   2. 4 words -> 1 word
1649
1650
   ltn     <     jn        jn  +2; jmp +4; br lab
1651
1652
   3. 4 words -> 2 words
1653
1654
   gt    >     jeq +2; jge label     jeq +6; jl  +4; br label
1655
   gtu     >     jeq +2; jhs label     jeq +6; jlo +4; br label
1656
1657
   4. 4 words -> 2 words and 2 labels
1658
1659
   leu     <=    jeq label; jlo label    jeq +2; jhs +4; br label
1660
   le    <=    jeq label; jl  label    jeq +2; jge +4; br label
1661
   =================================================================
1662
1663
   codemap for first cases is (labels masked ):
1664
        eq: 0x2002,0x4010,0x0000 -> 0x2400
1665
        ne: 0x2402,0x4010,0x0000 -> 0x2000
1666
        lt: 0x3402,0x4010,0x0000 -> 0x3800
1667
        ltu:  0x2c02,0x4010,0x0000 -> 0x2800
1668
        ge: 0x3802,0x4010,0x0000 -> 0x3400
1669
        geu:  0x2802,0x4010,0x0000 -> 0x2c00
1670
1671
  second case:
1672
        ltn:  0x3001,0x3c02,0x4010,0x0000 -> 0x3000
1673
1674
  third case:
1675
        gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
1676
        gtu:  0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
1677
1678
  fourth case:
1679
        leu:  0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
1680
        le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
1681
1682
  Unspecified case :)
1683
        jump: 0x4010,0x0000 -> 0x3c00.  */
1684
1685
0
#define NUMB_RELAX_CODES  12
1686
static struct rcodes_s
1687
{
1688
  int f0, f1;     /* From code.  */
1689
  int t0, t1;     /* To code.  */
1690
  int labels;     /* Position of labels: 1 - one label at first
1691
           word, 2 - one at second word, 3 - two
1692
           labels at both.  */
1693
  int cdx;      /* Words to match.  */
1694
  int bs;     /* Shrink bytes.  */
1695
  int off;      /* Offset from old label for new code.  */
1696
  int ncl;      /* New code length.  */
1697
} rcode[] =
1698
{/*         lab,cdx,bs,off,ncl */
1699
  { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2,  2},  /* jump */
1700
  { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4,  2},  /* eq */
1701
  { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4,  2},  /* ne */
1702
  { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4,  2},  /* lt */
1703
  { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4,  2},  /* ltu */
1704
  { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4,  2},  /* ge */
1705
  { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4,  2},  /* geu */
1706
  { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6,  2},  /* ltn */
1707
  { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6,  4},  /* gt */
1708
  { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6,  4},  /* gtu */
1709
  { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6,  4},  /* leu , 2 labels */
1710
  { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6,  4},  /* le  , 2 labels */
1711
  { 0,      0,      0,      0,      0, 0, 0, 0,  0}
1712
};
1713
1714
/* Return TRUE if a symbol exists at the given address.  */
1715
1716
static bool
1717
msp430_elf_symbol_address_p (bfd * abfd,
1718
           asection * sec,
1719
           Elf_Internal_Sym * isym,
1720
           bfd_vma addr)
1721
0
{
1722
0
  Elf_Internal_Shdr *symtab_hdr;
1723
0
  unsigned int sec_shndx;
1724
0
  Elf_Internal_Sym *isymend;
1725
0
  struct elf_link_hash_entry **sym_hashes;
1726
0
  struct elf_link_hash_entry **end_hashes;
1727
0
  unsigned int symcount;
1728
1729
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1730
1731
  /* Examine all the local symbols.  */
1732
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1733
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1734
0
    if (isym->st_shndx == sec_shndx && isym->st_value == addr)
1735
0
      return true;
1736
1737
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1738
0
        - symtab_hdr->sh_info);
1739
0
  sym_hashes = elf_sym_hashes (abfd);
1740
0
  end_hashes = sym_hashes + symcount;
1741
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1742
0
    {
1743
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1744
1745
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1746
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1747
0
    && sym_hash->root.u.def.section == sec
1748
0
    && sym_hash->root.u.def.value == addr)
1749
0
  return true;
1750
0
    }
1751
1752
0
  return false;
1753
0
}
1754
1755
/* Adjust all local symbols defined as '.section + 0xXXXX' (.section has
1756
   sec_shndx) referenced from current and other sections.  */
1757
1758
static bool
1759
msp430_elf_relax_adjust_locals (bfd * abfd, asection * sec, bfd_vma addr,
1760
        int count, unsigned int sec_shndx,
1761
        bfd_vma toaddr)
1762
0
{
1763
0
  Elf_Internal_Shdr *symtab_hdr;
1764
0
  Elf_Internal_Rela *irel;
1765
0
  Elf_Internal_Rela *irelend;
1766
0
  Elf_Internal_Sym *isym;
1767
1768
0
  irel = elf_section_data (sec)->relocs;
1769
0
  if (irel == NULL)
1770
0
    return true;
1771
1772
0
  irelend = irel + sec->reloc_count;
1773
0
  symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1774
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1775
1776
0
  for (;irel < irelend; irel++)
1777
0
    {
1778
0
      unsigned int sidx = ELF32_R_SYM(irel->r_info);
1779
0
      Elf_Internal_Sym *lsym = isym + sidx;
1780
1781
      /* Adjust symbols referenced by .sec+0xXX.  */
1782
0
      if (irel->r_addend > addr && irel->r_addend < toaddr
1783
0
    && sidx < symtab_hdr->sh_info
1784
0
    && lsym->st_shndx == sec_shndx)
1785
0
  irel->r_addend -= count;
1786
0
    }
1787
1788
0
  return true;
1789
0
}
1790
1791
/* Delete some bytes from a section while relaxing.  */
1792
1793
static bool
1794
msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
1795
             int count)
1796
0
{
1797
0
  Elf_Internal_Shdr *symtab_hdr;
1798
0
  unsigned int sec_shndx;
1799
0
  bfd_byte *contents;
1800
0
  Elf_Internal_Rela *irel;
1801
0
  Elf_Internal_Rela *irelend;
1802
0
  bfd_vma toaddr;
1803
0
  Elf_Internal_Sym *isym;
1804
0
  Elf_Internal_Sym *isymend;
1805
0
  struct elf_link_hash_entry **sym_hashes;
1806
0
  struct elf_link_hash_entry **end_hashes;
1807
0
  unsigned int symcount;
1808
0
  asection *p;
1809
1810
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1811
1812
0
  contents = elf_section_data (sec)->this_hdr.contents;
1813
1814
0
  toaddr = sec->size;
1815
0
  if (debug_relocs)
1816
0
    printf ("      deleting %d bytes between 0x%lx to 0x%lx\n",
1817
0
      count, (long) addr, (long) toaddr);
1818
1819
0
  irel = elf_section_data (sec)->relocs;
1820
0
  irelend = irel + sec->reloc_count;
1821
1822
  /* Actually delete the bytes.  */
1823
0
  memmove (contents + addr, contents + addr + count,
1824
0
     (size_t) (toaddr - addr - count));
1825
0
  sec->size -= count;
1826
1827
  /* Adjust all the relocs.  */
1828
0
  symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1829
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1830
0
  for (; irel < irelend; irel++)
1831
0
    {
1832
      /* Get the new reloc address.  */
1833
0
      if ((irel->r_offset > addr && irel->r_offset < toaddr))
1834
0
  irel->r_offset -= count;
1835
0
    }
1836
1837
0
  for (p = abfd->sections; p != NULL; p = p->next)
1838
0
    msp430_elf_relax_adjust_locals (abfd,p,addr,count,sec_shndx,toaddr);
1839
1840
  /* Adjust the local symbols defined in this section.  */
1841
0
  symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1842
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1843
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1844
0
    {
1845
0
      const char * name;
1846
1847
0
      name = bfd_elf_string_from_elf_section
1848
0
  (abfd, symtab_hdr->sh_link, isym->st_name);
1849
0
      name = name == NULL || *name == 0 ? bfd_section_name (sec) : name;
1850
1851
0
      if (isym->st_shndx != sec_shndx)
1852
0
  continue;
1853
1854
0
      if (isym->st_value > addr
1855
0
    && (isym->st_value < toaddr
1856
        /* We also adjust a symbol at the end of the section if its name is
1857
     on the list below.  These symbols are used for debug info
1858
     generation and they refer to the end of the current section, not
1859
     the start of the next section.  */
1860
0
        || (isym->st_value == toaddr
1861
0
      && name != NULL
1862
0
      && (startswith (name, ".Letext")
1863
0
          || startswith (name, ".LFE")))))
1864
0
  {
1865
0
    if (debug_relocs)
1866
0
      printf ("      adjusting value of local symbol %s from 0x%lx ",
1867
0
        name, (long) isym->st_value);
1868
0
    if (isym->st_value < addr + count)
1869
0
      isym->st_value = addr;
1870
0
    else
1871
0
      isym->st_value -= count;
1872
0
    if (debug_relocs)
1873
0
      printf ("to 0x%lx\n", (long) isym->st_value);
1874
0
  }
1875
      /* Adjust the function symbol's size as well.  */
1876
0
      else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC
1877
0
         && isym->st_value + isym->st_size > addr
1878
0
         && isym->st_value + isym->st_size < toaddr)
1879
0
  isym->st_size -= count;
1880
0
    }
1881
1882
  /* Now adjust the global symbols defined in this section.  */
1883
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1884
0
        - symtab_hdr->sh_info);
1885
0
  sym_hashes = elf_sym_hashes (abfd);
1886
0
  end_hashes = sym_hashes + symcount;
1887
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1888
0
    {
1889
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1890
1891
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1892
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1893
0
    && sym_hash->root.u.def.section == sec
1894
0
    && sym_hash->root.u.def.value > addr
1895
0
    && sym_hash->root.u.def.value < toaddr)
1896
0
  {
1897
0
    if (sym_hash->root.u.def.value < addr + count)
1898
0
      sym_hash->root.u.def.value = addr;
1899
0
    else
1900
0
      sym_hash->root.u.def.value -= count;
1901
0
  }
1902
      /* Adjust the function symbol's size as well.  */
1903
0
      else if (sym_hash->root.type == bfd_link_hash_defined
1904
0
         && sym_hash->root.u.def.section == sec
1905
0
         && sym_hash->type == STT_FUNC
1906
0
         && sym_hash->root.u.def.value + sym_hash->size > addr
1907
0
         && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1908
0
  sym_hash->size -= count;
1909
0
    }
1910
1911
0
  return true;
1912
0
}
1913
1914
/* Insert one or two words into a section whilst relaxing.  */
1915
1916
static bfd_byte *
1917
msp430_elf_relax_add_words (bfd * abfd, asection * sec, bfd_vma addr,
1918
          int num_words, int word1, int word2)
1919
0
{
1920
0
  Elf_Internal_Shdr *symtab_hdr;
1921
0
  unsigned int sec_shndx;
1922
0
  bfd_byte *contents;
1923
0
  Elf_Internal_Rela *irel;
1924
0
  Elf_Internal_Rela *irelend;
1925
0
  Elf_Internal_Sym *isym;
1926
0
  Elf_Internal_Sym *isymend;
1927
0
  struct elf_link_hash_entry **sym_hashes;
1928
0
  struct elf_link_hash_entry **end_hashes;
1929
0
  unsigned int symcount;
1930
0
  bfd_vma sec_end;
1931
0
  asection *p;
1932
0
  if (debug_relocs)
1933
0
    printf ("      adding %d words at 0x%lx\n", num_words,
1934
0
      (long) (sec->output_section->vma + sec->output_offset + addr));
1935
1936
0
  contents = elf_section_data (sec)->this_hdr.contents;
1937
0
  sec_end = sec->size;
1938
0
  int num_bytes = num_words * 2;
1939
1940
  /* Make space for the new words.  */
1941
0
  contents = bfd_realloc (contents, sec_end + num_bytes);
1942
0
  memmove (contents + addr + num_bytes, contents + addr, sec_end - addr);
1943
1944
  /* Insert the new words.  */
1945
0
  bfd_put_16 (abfd, word1, contents + addr);
1946
0
  if (num_words == 2)
1947
0
    bfd_put_16 (abfd, word2, contents + addr + 2);
1948
1949
  /* Update the section information.  */
1950
0
  sec->size += num_bytes;
1951
0
  elf_section_data (sec)->this_hdr.contents = contents;
1952
1953
  /* Adjust all the relocs.  */
1954
0
  irel = elf_section_data (sec)->relocs;
1955
0
  irelend = irel + sec->reloc_count;
1956
1957
0
  for (; irel < irelend; irel++)
1958
0
    if ((irel->r_offset >= addr && irel->r_offset < sec_end))
1959
0
      irel->r_offset += num_bytes;
1960
1961
  /* Adjust the local symbols defined in this section.  */
1962
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1963
0
  for (p = abfd->sections; p != NULL; p = p->next)
1964
0
    msp430_elf_relax_adjust_locals (abfd, p, addr, -num_bytes,
1965
0
            sec_shndx, sec_end);
1966
1967
  /* Adjust the global symbols affected by the move.  */
1968
0
  symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1969
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1970
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1971
0
    if (isym->st_shndx == sec_shndx
1972
0
  && isym->st_value >= addr && isym->st_value < sec_end)
1973
0
      {
1974
0
  if (debug_relocs)
1975
0
    printf ("      adjusting value of local symbol %s from 0x%lx to "
1976
0
      "0x%lx\n", bfd_elf_string_from_elf_section
1977
0
      (abfd, symtab_hdr->sh_link, isym->st_name),
1978
0
      (long) isym->st_value, (long)(isym->st_value + num_bytes));
1979
0
  isym->st_value += num_bytes;
1980
0
      }
1981
1982
  /* Now adjust the global symbols defined in this section.  */
1983
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1984
0
        - symtab_hdr->sh_info);
1985
0
  sym_hashes = elf_sym_hashes (abfd);
1986
0
  end_hashes = sym_hashes + symcount;
1987
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1988
0
    {
1989
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1990
1991
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1992
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1993
0
    && sym_hash->root.u.def.section == sec
1994
0
    && sym_hash->root.u.def.value >= addr
1995
0
    && sym_hash->root.u.def.value < sec_end)
1996
0
  sym_hash->root.u.def.value += num_bytes;
1997
0
    }
1998
1999
0
  return contents;
2000
0
}
2001
2002
static bool
2003
msp430_elf_relax_section (bfd * abfd, asection * sec,
2004
        struct bfd_link_info * link_info,
2005
        bool * again)
2006
0
{
2007
0
  Elf_Internal_Shdr * symtab_hdr;
2008
0
  Elf_Internal_Rela * internal_relocs;
2009
0
  Elf_Internal_Rela * irel;
2010
0
  Elf_Internal_Rela * irelend;
2011
0
  bfd_byte *        contents = NULL;
2012
0
  Elf_Internal_Sym *  isymbuf = NULL;
2013
2014
  /* Assume nothing changes.  */
2015
0
  *again = false;
2016
2017
  /* We don't have to do anything for a relocatable link, if
2018
     this section does not have relocs, or if this is not a
2019
     code section.  */
2020
0
  if (bfd_link_relocatable (link_info)
2021
0
      || sec->reloc_count == 0
2022
0
      || (sec->flags & SEC_RELOC) == 0
2023
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
2024
0
      || (sec->flags & SEC_CODE) == 0)
2025
0
    return true;
2026
2027
0
  if (debug_relocs)
2028
0
    printf ("Relaxing %s (%p), output_offset: 0x%lx sec size: 0x%lx\n",
2029
0
      sec->name, sec, (long) sec->output_offset, (long) sec->size);
2030
2031
0
  symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2032
2033
  /* Get a copy of the native relocations.  */
2034
0
  internal_relocs =
2035
0
    _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
2036
0
  if (internal_relocs == NULL)
2037
0
    goto error_return;
2038
2039
  /* Walk through them looking for relaxing opportunities.  */
2040
0
  irelend = internal_relocs + sec->reloc_count;
2041
2042
0
  if (debug_relocs)
2043
0
    printf ("  trying code size growing relocs\n");
2044
  /* Do code size growing relocs first.  */
2045
0
  for (irel = internal_relocs; irel < irelend; irel++)
2046
0
    {
2047
0
      bfd_vma symval;
2048
2049
      /* If this isn't something that can be relaxed, then ignore
2050
   this reloc.  */
2051
0
      if (uses_msp430x_relocs (abfd)
2052
0
    && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430X_10_PCREL)
2053
0
  ;
2054
0
      else if (! uses_msp430x_relocs (abfd)
2055
0
         && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_10_PCREL)
2056
0
  ;
2057
0
      else
2058
0
  continue;
2059
2060
      /* Get the section contents if we haven't done so already.  */
2061
0
      if (contents == NULL)
2062
0
  {
2063
    /* Get cached copy if it exists.  */
2064
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
2065
0
      contents = elf_section_data (sec)->this_hdr.contents;
2066
0
    else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2067
0
      goto error_return;
2068
0
  }
2069
2070
      /* Read this BFD's local symbols if we haven't done so already.  */
2071
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2072
0
  {
2073
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2074
0
    if (isymbuf == NULL)
2075
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2076
0
              symtab_hdr->sh_info, 0,
2077
0
              NULL, NULL, NULL);
2078
0
    if (isymbuf == NULL)
2079
0
      goto error_return;
2080
0
  }
2081
2082
      /* Get the value of the symbol referred to by the reloc.  */
2083
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2084
0
  {
2085
    /* A local symbol.  */
2086
0
    Elf_Internal_Sym *isym;
2087
0
    asection *sym_sec;
2088
2089
0
    isym = isymbuf + ELF32_R_SYM (irel->r_info);
2090
0
    if (isym->st_shndx == SHN_UNDEF)
2091
0
      sym_sec = bfd_und_section_ptr;
2092
0
    else if (isym->st_shndx == SHN_ABS)
2093
0
      sym_sec = bfd_abs_section_ptr;
2094
0
    else if (isym->st_shndx == SHN_COMMON)
2095
0
      sym_sec = bfd_com_section_ptr;
2096
0
    else
2097
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2098
0
    symval = (isym->st_value
2099
0
        + sym_sec->output_section->vma + sym_sec->output_offset);
2100
2101
0
    if (debug_relocs)
2102
0
      printf ("    processing reloc at 0x%lx for local sym: %s "
2103
0
        "st_value: 0x%lx adj value: 0x%lx\n",
2104
0
        (long) (sec->output_offset + sec->output_section->vma
2105
0
          + irel->r_offset),
2106
0
        bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
2107
0
                 isym->st_name),
2108
0
        (long) isym->st_value, (long) symval);
2109
0
  }
2110
0
      else
2111
0
  {
2112
0
    unsigned long indx;
2113
0
    struct elf_link_hash_entry *h;
2114
2115
    /* An external symbol.  */
2116
0
    indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2117
0
    h = elf_sym_hashes (abfd)[indx];
2118
0
    BFD_ASSERT (h != NULL);
2119
2120
0
    if (h->root.type != bfd_link_hash_defined
2121
0
        && h->root.type != bfd_link_hash_defweak)
2122
      /* This appears to be a reference to an undefined
2123
         symbol.  Just ignore it--it will be caught by the
2124
         regular reloc processing.  */
2125
0
      continue;
2126
2127
0
    symval = (h->root.u.def.value
2128
0
        + h->root.u.def.section->output_section->vma
2129
0
        + h->root.u.def.section->output_offset);
2130
0
    if (debug_relocs)
2131
0
      printf ("    processing reloc at 0x%lx for global sym: %s "
2132
0
        "st_value: 0x%lx adj value: 0x%lx\n",
2133
0
        (long) (sec->output_offset + sec->output_section->vma
2134
0
        + irel->r_offset),
2135
0
        h->root.root.string, (long) h->root.u.def.value,
2136
0
        (long) symval);
2137
0
  }
2138
2139
      /* For simplicity of coding, we are going to modify the section
2140
   contents, the section relocs, and the BFD symbol table.  We
2141
   must tell the rest of the code not to free up this
2142
   information.  It would be possible to instead create a table
2143
   of changes which have to be made, as is done in coff-mips.c;
2144
   that would be more work, but would require less memory when
2145
   the linker is run.  */
2146
2147
0
      bfd_signed_vma value = symval;
2148
0
      int opcode;
2149
2150
      /* Compute the value that will be relocated.  */
2151
0
      value += irel->r_addend;
2152
      /* Convert to PC relative.  */
2153
0
      value -= (sec->output_section->vma + sec->output_offset);
2154
0
      value -= irel->r_offset;
2155
0
      value -= 2;
2156
2157
      /* Scale.  */
2158
0
      value >>= 1;
2159
2160
      /* If it is in range then no modifications are needed.  */
2161
0
      if (value >= -512 && value <= 511)
2162
0
  continue;
2163
2164
      /* Get the opcode.  */
2165
0
      opcode = bfd_get_16 (abfd, contents + irel->r_offset);
2166
2167
      /* Compute the new opcode.  We are going to convert:
2168
   JMP label
2169
     into:
2170
   BR[A] label
2171
     or
2172
   J<cond> label
2173
     into:
2174
   J<inv-cond> 1f
2175
   BR[A] #label
2176
   1:     */
2177
0
      switch (opcode & 0xfc00)
2178
0
  {
2179
0
  case 0x3800: opcode = 0x3402; break; /* Jl  -> Jge +2 */
2180
0
  case 0x3400: opcode = 0x3802; break; /* Jge -> Jl  +2 */
2181
0
  case 0x2c00: opcode = 0x2802; break; /* Jhs -> Jlo +2 */
2182
0
  case 0x2800: opcode = 0x2c02; break; /* Jlo -> Jhs +2 */
2183
0
  case 0x2400: opcode = 0x2002; break; /* Jeq -> Jne +2 */
2184
0
  case 0x2000: opcode = 0x2402; break; /* jne -> Jeq +2 */
2185
0
  case 0x3000: /* jn    */
2186
    /* There is no direct inverse of the Jn insn.
2187
       FIXME: we could do this as:
2188
    Jn 1f
2189
    br 2f
2190
       1: br label
2191
       2:          */
2192
0
    continue;
2193
0
  case 0x3c00:
2194
0
    if (uses_msp430x_relocs (abfd))
2195
0
      opcode = 0x0080; /* JMP -> BRA  */
2196
0
    else
2197
0
      opcode = 0x4030; /* JMP -> BR  */
2198
0
    break;
2199
0
  default:
2200
    /* Unhandled branch instruction.  */
2201
    /* fprintf (stderr, "unrecog: %x\n", opcode); */
2202
0
    continue;
2203
0
  }
2204
2205
      /* Note that we've changed the relocs, section contents, etc.  */
2206
0
      elf_section_data (sec)->relocs = internal_relocs;
2207
0
      elf_section_data (sec)->this_hdr.contents = contents;
2208
0
      symtab_hdr->contents = (unsigned char *) isymbuf;
2209
2210
      /* Install the new opcode.  */
2211
0
      bfd_put_16 (abfd, opcode, contents + irel->r_offset);
2212
2213
      /* Insert the new branch instruction.  */
2214
0
      if (uses_msp430x_relocs (abfd))
2215
0
  {
2216
0
    if (debug_relocs)
2217
0
      printf ("      R_MSP430X_10_PCREL -> R_MSP430X_ABS20_ADR_SRC "
2218
0
        "(growing with new opcode 0x%x)\n", opcode);
2219
2220
    /* Insert an absolute branch (aka MOVA) instruction.
2221
       Note that bits 19:16 of the address are stored in the first word
2222
       of the insn, so this is where r_offset will point to.  */
2223
0
    if (opcode == 0x0080)
2224
0
      {
2225
        /* If we're inserting a BRA because we are converting from a JMP,
2226
     then only add one word for destination address; the BRA opcode
2227
     has already been written.  */
2228
0
        contents = msp430_elf_relax_add_words
2229
0
    (abfd, sec, irel->r_offset + 2, 1, 0x0000, 0);
2230
0
      }
2231
0
    else
2232
0
      {
2233
0
        contents = msp430_elf_relax_add_words
2234
0
    (abfd, sec, irel->r_offset + 2, 2, 0x0080, 0x0000);
2235
        /* Update the relocation to point to the inserted branch
2236
     instruction.  Note - we are changing a PC-relative reloc
2237
     into an absolute reloc, but this is OK because we have
2238
     arranged with the assembler to have the reloc's value be
2239
     a (local) symbol, not a section+offset value.  */
2240
0
        irel->r_offset += 2;
2241
0
      }
2242
2243
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2244
0
               R_MSP430X_ABS20_ADR_SRC);
2245
0
  }
2246
0
      else
2247
0
  {
2248
0
    if (debug_relocs)
2249
0
      printf ("      R_MSP430_10_PCREL -> R_MSP430_16 "
2250
0
        "(growing with new opcode 0x%x)\n", opcode);
2251
0
    if (opcode == 0x4030)
2252
0
      {
2253
        /* If we're inserting a BR because we are converting from a JMP,
2254
     then only add one word for destination address; the BR opcode
2255
     has already been written.  */
2256
0
        contents = msp430_elf_relax_add_words
2257
0
    (abfd, sec, irel->r_offset + 2, 1, 0x0000, 0);
2258
0
        irel->r_offset += 2;
2259
0
      }
2260
0
    else
2261
0
      {
2262
0
        contents = msp430_elf_relax_add_words
2263
0
    (abfd, sec, irel->r_offset + 2, 2, 0x4030, 0x0000);
2264
        /* See comment above about converting a 10-bit PC-rel
2265
     relocation into a 16-bit absolute relocation.  */
2266
0
        irel->r_offset += 4;
2267
0
      }
2268
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2269
0
               R_MSP430_16);
2270
0
  }
2271
2272
      /* Growing the section may mean that other
2273
   conditional branches need to be fixed.  */
2274
0
      *again = true;
2275
0
    }
2276
2277
0
    if (debug_relocs)
2278
0
      printf ("  trying code size shrinking relocs\n");
2279
2280
0
    for (irel = internal_relocs; irel < irelend; irel++)
2281
0
      {
2282
0
  bfd_vma symval;
2283
2284
  /* Get the section contents if we haven't done so already.  */
2285
0
  if (contents == NULL)
2286
0
    {
2287
      /* Get cached copy if it exists.  */
2288
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
2289
0
        contents = elf_section_data (sec)->this_hdr.contents;
2290
0
      else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2291
0
        goto error_return;
2292
0
    }
2293
2294
  /* Read this BFD's local symbols if we haven't done so already.  */
2295
0
  if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2296
0
    {
2297
0
      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2298
0
      if (isymbuf == NULL)
2299
0
        isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2300
0
                symtab_hdr->sh_info, 0,
2301
0
                NULL, NULL, NULL);
2302
0
      if (isymbuf == NULL)
2303
0
        goto error_return;
2304
0
    }
2305
2306
  /* Get the value of the symbol referred to by the reloc.  */
2307
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2308
0
    {
2309
      /* A local symbol.  */
2310
0
      Elf_Internal_Sym *isym;
2311
0
      asection *sym_sec;
2312
2313
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
2314
0
      if (isym->st_shndx == SHN_UNDEF)
2315
0
        sym_sec = bfd_und_section_ptr;
2316
0
      else if (isym->st_shndx == SHN_ABS)
2317
0
        sym_sec = bfd_abs_section_ptr;
2318
0
      else if (isym->st_shndx == SHN_COMMON)
2319
0
        sym_sec = bfd_com_section_ptr;
2320
0
      else
2321
0
        sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2322
0
      symval = (isym->st_value
2323
0
          + sym_sec->output_section->vma + sym_sec->output_offset);
2324
2325
0
      if (debug_relocs)
2326
0
        printf ("    processing reloc at 0x%lx for local sym: %s "
2327
0
          "st_value: 0x%lx adj value: 0x%lx\n",
2328
0
          (long) (sec->output_offset + sec->output_section->vma
2329
0
            + irel->r_offset),
2330
0
          bfd_elf_string_from_elf_section
2331
0
          (abfd, symtab_hdr->sh_link, isym->st_name),
2332
0
          (long) isym->st_value, (long) symval);
2333
0
    }
2334
0
  else
2335
0
    {
2336
0
      unsigned long indx;
2337
0
      struct elf_link_hash_entry *h;
2338
2339
      /* An external symbol.  */
2340
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2341
0
      h = elf_sym_hashes (abfd)[indx];
2342
0
      BFD_ASSERT (h != NULL);
2343
2344
0
      if (h->root.type != bfd_link_hash_defined
2345
0
    && h->root.type != bfd_link_hash_defweak)
2346
        /* This appears to be a reference to an undefined
2347
     symbol.  Just ignore it--it will be caught by the
2348
     regular reloc processing.  */
2349
0
        continue;
2350
2351
0
      symval = (h->root.u.def.value
2352
0
          + h->root.u.def.section->output_section->vma
2353
0
          + h->root.u.def.section->output_offset);
2354
0
      if (debug_relocs)
2355
0
        printf ("    processing reloc at 0x%lx for global sym: %s "
2356
0
          "st_value: 0x%lx adj value: 0x%lx\n", (long)
2357
0
          (sec->output_offset + sec->output_section->vma
2358
0
           + irel->r_offset),
2359
0
          h->root.root.string, (long) h->root.u.def.value,
2360
0
          (long) symval);
2361
0
    }
2362
2363
  /* For simplicity of coding, we are going to modify the section
2364
     contents, the section relocs, and the BFD symbol table.  We
2365
     must tell the rest of the code not to free up this
2366
     information.  It would be possible to instead create a table
2367
     of changes which have to be made, as is done in coff-mips.c;
2368
     that would be more work, but would require less memory when
2369
     the linker is run.  */
2370
2371
  /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
2372
     branch.  */
2373
  /* Paranoia? paranoia...  */
2374
0
  if (! uses_msp430x_relocs (abfd)
2375
0
      && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_RL_PCREL)
2376
0
    {
2377
0
      bfd_vma value = symval;
2378
2379
      /* Deal with pc-relative gunk.  */
2380
0
      value -= (sec->output_section->vma + sec->output_offset);
2381
0
      value -= irel->r_offset;
2382
0
      value += irel->r_addend;
2383
2384
      /* See if the value will fit in 10 bits, note the high value is
2385
         1016 as the target will be two bytes closer if we are
2386
         able to relax.  */
2387
0
      if ((long) value < 1016 && (long) value > -1016)
2388
0
        {
2389
0
    int code0 = 0, code1 = 0, code2 = 0;
2390
0
    int i;
2391
0
    struct rcodes_s *rx;
2392
2393
    /* Get the opcode.  */
2394
0
    if (irel->r_offset >= 6)
2395
0
      code0 = bfd_get_16 (abfd, contents + irel->r_offset - 6);
2396
2397
0
    if (irel->r_offset >= 4)
2398
0
      code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
2399
2400
0
    code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
2401
2402
0
    if (code2 != 0x4010)
2403
0
      continue;
2404
2405
    /* Check r4 and r3.  */
2406
0
    for (i = NUMB_RELAX_CODES - 1; i >= 0; i--)
2407
0
      {
2408
0
        rx = &rcode[i];
2409
0
        if (rx->cdx == 2 && rx->f0 == code0 && rx->f1 == code1)
2410
0
          break;
2411
0
        else if (rx->cdx == 1 && rx->f1 == code1)
2412
0
          break;
2413
0
        else if (rx->cdx == 0) /* This is an unconditional jump.  */
2414
0
          break;
2415
0
      }
2416
2417
    /* Check labels:
2418
       .Label0:       ; we do not care about this label
2419
       jeq    +6
2420
       .Label1:       ; make sure there is no label here
2421
       jl     +4
2422
       .Label2:       ; make sure there is no label here
2423
       br .Label_dst
2424
2425
       So, if there is .Label1 or .Label2 we cannot relax this code.
2426
       This actually should not happen, cause for relaxable
2427
       instructions we use RL_PCREL reloc instead of 16_PCREL.
2428
       Will change this in the future. */
2429
2430
0
    if (rx->cdx > 0
2431
0
        && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
2432
0
                irel->r_offset - 2))
2433
0
      continue;
2434
0
    if (rx->cdx > 1
2435
0
        && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
2436
0
                irel->r_offset - 4))
2437
0
      continue;
2438
2439
    /* Note that we've changed the relocs, section contents, etc.  */
2440
0
    elf_section_data (sec)->relocs = internal_relocs;
2441
0
    elf_section_data (sec)->this_hdr.contents = contents;
2442
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
2443
2444
0
    if (debug_relocs)
2445
0
      printf ("      R_MSP430_RL_PCREL -> ");
2446
    /* Fix the relocation's type.  */
2447
0
    if (uses_msp430x_relocs (abfd))
2448
0
      {
2449
0
        if (rx->labels == 3) /* Handle special cases.  */
2450
0
          irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2451
0
               R_MSP430X_2X_PCREL);
2452
0
        else
2453
0
          irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2454
0
               R_MSP430X_10_PCREL);
2455
0
      }
2456
0
    else
2457
0
      {
2458
0
        if (rx->labels == 3) /* Handle special cases.  */
2459
0
          {
2460
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2461
0
                 R_MSP430_2X_PCREL);
2462
0
      if (debug_relocs)
2463
0
        printf ("R_MSP430_2X_PCREL (shrinking with new opcode"
2464
0
          " 0x%x)\n", rx->t0);
2465
0
          }
2466
0
        else
2467
0
          {
2468
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2469
0
                 R_MSP430_10_PCREL);
2470
0
      if (debug_relocs)
2471
0
        printf ("R_MSP430_10_PCREL (shrinking with new opcode"
2472
0
          " 0x%x)\n", rx->t0);
2473
0
          }
2474
0
      }
2475
2476
    /* Fix the opcode right way.  */
2477
0
    bfd_put_16 (abfd, rx->t0, contents + irel->r_offset - rx->off);
2478
0
    if (rx->t1)
2479
0
      bfd_put_16 (abfd, rx->t1,
2480
0
            contents + irel->r_offset - rx->off + 2);
2481
2482
    /* Delete bytes. */
2483
0
    if (!msp430_elf_relax_delete_bytes (abfd, sec,
2484
0
                irel->r_offset - rx->off +
2485
0
                rx->ncl, rx->bs))
2486
0
      goto error_return;
2487
2488
    /* Handle unconditional jumps.  */
2489
0
    if (rx->cdx == 0)
2490
0
      irel->r_offset -= 2;
2491
2492
    /* That will change things, so, we should relax again.
2493
       Note that this is not required, and it may be slow.  */
2494
0
    *again = true;
2495
0
        }
2496
0
    }
2497
2498
  /* Try to turn a 16-bit absolute branch into a 10-bit pc-relative
2499
     branch.  */
2500
0
  if ((uses_msp430x_relocs (abfd)
2501
0
       && ELF32_R_TYPE (irel->r_info) == R_MSP430X_ABS16)
2502
0
      || (! uses_msp430x_relocs (abfd)
2503
0
    && ELF32_R_TYPE (irel->r_info) == R_MSP430_16))
2504
0
    {
2505
0
      bfd_vma value = symval;
2506
2507
0
      value -= (sec->output_section->vma + sec->output_offset);
2508
0
      value -= irel->r_offset;
2509
0
      value += irel->r_addend;
2510
2511
      /* See if the value will fit in 10 bits, note the high value is
2512
         1016 as the target will be two bytes closer if we are
2513
         able to relax.  */
2514
0
      if ((long) value < 1016 && (long) value > -1016)
2515
0
        {
2516
0
    int code1, code2, opcode;
2517
2518
    /* Get the opcode.  */
2519
0
    code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
2520
0
    if (code2 != 0x4030) /* BR -> JMP */
2521
0
      continue;
2522
    /* FIXME: check r4 and r3 ? */
2523
    /* FIXME: Handle 0x4010 as well ?  */
2524
2525
    /* Note that we've changed the relocs, section contents, etc.  */
2526
0
    elf_section_data (sec)->relocs = internal_relocs;
2527
0
    elf_section_data (sec)->this_hdr.contents = contents;
2528
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
2529
2530
    /* Fix the relocation's type.  */
2531
0
    if (uses_msp430x_relocs (abfd))
2532
0
      {
2533
0
        irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2534
0
             R_MSP430X_10_PCREL);
2535
0
        if (debug_relocs)
2536
0
          printf ("      R_MSP430X_16 -> R_MSP430X_10_PCREL ");
2537
0
      }
2538
0
    else
2539
0
      {
2540
0
        irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2541
0
             R_MSP430_10_PCREL);
2542
0
        if (debug_relocs)
2543
0
          printf ("      R_MSP430_16 -> R_MSP430_10_PCREL ");
2544
0
      }
2545
    /* If we're trying to shrink a BR[A] after previously having
2546
       grown a JMP for this reloc, then we have a sequence like
2547
       this:
2548
         J<cond> 1f
2549
         BR[A]
2550
         1:
2551
       The opcode for J<cond> has the target hard-coded as 2 words
2552
       ahead of the insn, instead of using a reloc.
2553
       This means we cannot rely on any of the helper functions to
2554
       update this hard-coded jump destination if we remove the
2555
       BR[A] insn, so we must explicitly update it here.
2556
       This does mean that we can remove the entire branch
2557
       instruction, and invert the conditional jump, saving us 4
2558
       bytes rather than only 2 if we detected this in the normal
2559
       way.  */
2560
0
    code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
2561
0
    switch (code1)
2562
0
      {
2563
0
        case 0x3802: opcode = 0x3401; break; /* Jl  +2 -> Jge +1 */
2564
0
        case 0x3402: opcode = 0x3801; break; /* Jge +2 -> Jl  +1 */
2565
0
        case 0x2c02: opcode = 0x2801; break; /* Jhs +2 -> Jlo +1 */
2566
0
        case 0x2802: opcode = 0x2c01; break; /* Jlo +2 -> Jhs +1 */
2567
0
        case 0x2402: opcode = 0x2001; break; /* Jeq +2 -> Jne +1 */
2568
0
        case 0x2002: opcode = 0x2401; break; /* jne +2 -> Jeq +1 */
2569
0
        case 0x3002: /* jn +2   */
2570
          /* FIXME: There is no direct inverse of the Jn insn.  */
2571
0
          continue;
2572
0
        default:
2573
          /* The previous opcode does not have a hard-coded jump
2574
       that we added when previously relaxing, so relax the
2575
       current branch as normal.  */
2576
0
          opcode = 0x3c00;
2577
0
          break;
2578
0
        }
2579
0
    if (debug_relocs)
2580
0
      printf ("(shrinking with new opcode 0x%x)\n", opcode);
2581
2582
0
    if (opcode != 0x3c00)
2583
0
      {
2584
        /* Invert the opcode of the conditional jump.  */
2585
0
        bfd_put_16 (abfd, opcode, contents + irel->r_offset - 4);
2586
0
        irel->r_offset -= 4;
2587
2588
        /* Delete 4 bytes - the full BR insn.  */
2589
0
        if (!msp430_elf_relax_delete_bytes (abfd, sec,
2590
0
              irel->r_offset + 2, 4))
2591
0
          goto error_return;
2592
0
      }
2593
0
    else
2594
0
      {
2595
        /* Fix the opcode right way.  */
2596
0
        bfd_put_16 (abfd, opcode, contents + irel->r_offset - 2);
2597
0
        irel->r_offset -= 2;
2598
2599
        /* Delete bytes.  */
2600
0
        if (!msp430_elf_relax_delete_bytes (abfd, sec,
2601
0
              irel->r_offset + 2, 2))
2602
0
          goto error_return;
2603
0
      }
2604
2605
    /* That will change things, so, we should relax again.
2606
       Note that this is not required, and it may be slow.  */
2607
0
    *again = true;
2608
0
        }
2609
0
    }
2610
0
      }
2611
2612
0
  if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2613
0
    {
2614
0
      if (!link_info->keep_memory)
2615
0
  free (isymbuf);
2616
0
      else
2617
0
  {
2618
    /* Cache the symbols for elf_link_input_bfd.  */
2619
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
2620
0
  }
2621
0
    }
2622
2623
0
  if (contents != NULL
2624
0
      && elf_section_data (sec)->this_hdr.contents != contents)
2625
0
    {
2626
0
      if (!link_info->keep_memory)
2627
0
  free (contents);
2628
0
      else
2629
0
  {
2630
    /* Cache the section contents for elf_link_input_bfd.  */
2631
0
    elf_section_data (sec)->this_hdr.contents = contents;
2632
0
  }
2633
0
    }
2634
2635
0
  if (elf_section_data (sec)->relocs != internal_relocs)
2636
0
    free (internal_relocs);
2637
2638
0
  return true;
2639
2640
0
 error_return:
2641
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
2642
0
    free (isymbuf);
2643
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
2644
0
    free (contents);
2645
0
  if (elf_section_data (sec)->relocs != internal_relocs)
2646
0
    free (internal_relocs);
2647
2648
0
  return false;
2649
0
}
2650
2651
/* Handle an MSP430 specific section when reading an object file.
2652
   This is called when bfd_section_from_shdr finds a section with
2653
   an unknown type.  */
2654
2655
static bool
2656
elf32_msp430_section_from_shdr (bfd *abfd,
2657
        Elf_Internal_Shdr * hdr,
2658
        const char *name,
2659
        int shindex)
2660
63
{
2661
63
  switch (hdr->sh_type)
2662
63
    {
2663
3
    case SHT_MSP430_SEC_FLAGS:
2664
3
    case SHT_MSP430_SYM_ALIASES:
2665
3
    case SHT_MSP430_ATTRIBUTES:
2666
3
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2667
60
    default:
2668
60
      return false;
2669
63
    }
2670
63
}
2671
2672
static bool
2673
elf32_msp430_obj_attrs_handle_unknown (bfd *abfd, int tag)
2674
0
{
2675
0
  _bfd_error_handler
2676
    /* xgettext:c-format */
2677
0
    (_("warning: %pB: unknown MSPABI object attribute %d"),
2678
0
     abfd, tag);
2679
0
  return true;
2680
0
}
2681
2682
/* Determine whether an object attribute tag takes an integer, a
2683
   string or both.  */
2684
2685
static int
2686
elf32_msp430_obj_attrs_arg_type (obj_attr_tag_t tag)
2687
0
{
2688
0
  if (tag == Tag_compatibility)
2689
0
    return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
2690
2691
0
  if (tag < 32)
2692
0
    return ATTR_TYPE_FLAG_INT_VAL;
2693
2694
0
  return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
2695
0
}
2696
2697
static inline const char *
2698
isa_type (int isa)
2699
0
{
2700
0
  switch (isa)
2701
0
    {
2702
0
    case 1: return "MSP430";
2703
0
    case 2: return "MSP430X";
2704
0
    default: return "unknown";
2705
0
    }
2706
0
}
2707
2708
static inline const char *
2709
code_model (int model)
2710
0
{
2711
0
  switch (model)
2712
0
    {
2713
0
    case 1: return "small";
2714
0
    case 2: return "large";
2715
0
    default: return "unknown";
2716
0
    }
2717
0
}
2718
2719
static inline const char *
2720
data_model (int model)
2721
0
{
2722
0
  switch (model)
2723
0
    {
2724
0
    case 1: return "small";
2725
0
    case 2: return "large";
2726
0
    case 3: return "restricted large";
2727
0
    default: return "unknown";
2728
0
    }
2729
0
}
2730
2731
/* Merge MSPABI and GNU object attributes from IBFD into OBFD.
2732
   Raise an error if there are conflicting attributes.  */
2733
2734
static bool
2735
elf32_msp430_merge_msp430_attributes (bfd *ibfd, struct bfd_link_info *info)
2736
0
{
2737
0
  bfd *obfd = info->output_bfd;
2738
0
  obj_attribute *in_msp_attr, *in_gnu_attr;
2739
0
  obj_attribute *out_msp_attr, *out_gnu_attr;
2740
0
  bool result = true;
2741
0
  static bfd * first_input_bfd = NULL;
2742
2743
  /* Skip linker created files.  */
2744
0
  if (ibfd->flags & BFD_LINKER_CREATED)
2745
0
    return true;
2746
2747
  /* LTO can create temporary files for linking which may not have an attribute
2748
     section.  */
2749
0
  if (ibfd->lto_output
2750
0
      && bfd_get_section_by_name (ibfd, ".MSP430.attributes") == NULL)
2751
0
    return true;
2752
2753
  /* If this is the first real object just copy the attributes.  */
2754
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
2755
0
    {
2756
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
2757
2758
0
      out_msp_attr = elf_known_obj_attributes_proc (obfd);
2759
2760
      /* Use the Tag_null value to indicate that
2761
   the attributes have been initialized.  */
2762
0
      out_msp_attr[0].i = 1;
2763
2764
0
      first_input_bfd = ibfd;
2765
0
      return true;
2766
0
    }
2767
2768
0
  in_msp_attr = elf_known_obj_attributes_proc (ibfd);
2769
0
  out_msp_attr = elf_known_obj_attributes_proc (obfd);
2770
0
  in_gnu_attr = elf_known_obj_attributes (ibfd) [OBJ_ATTR_GNU];
2771
0
  out_gnu_attr = elf_known_obj_attributes (obfd) [OBJ_ATTR_GNU];
2772
2773
  /* The ISAs must be the same.  */
2774
0
  if (in_msp_attr[OFBA_MSPABI_Tag_ISA].i != out_msp_attr[OFBA_MSPABI_Tag_ISA].i)
2775
0
    {
2776
0
      _bfd_error_handler
2777
  /* xgettext:c-format */
2778
0
  (_("error: %pB uses %s instructions but %pB uses %s"),
2779
0
   ibfd, isa_type (in_msp_attr[OFBA_MSPABI_Tag_ISA].i),
2780
0
   first_input_bfd, isa_type (out_msp_attr[OFBA_MSPABI_Tag_ISA].i));
2781
0
      result = false;
2782
0
    }
2783
2784
  /* The code models must be the same.  */
2785
0
  if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i
2786
0
      != out_msp_attr[OFBA_MSPABI_Tag_Code_Model].i)
2787
0
    {
2788
0
      _bfd_error_handler
2789
  /* xgettext:c-format */
2790
0
  (_("error: %pB uses the %s code model whereas %pB uses the %s code model"),
2791
0
   ibfd, code_model (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i),
2792
0
   first_input_bfd,
2793
0
   code_model (out_msp_attr[OFBA_MSPABI_Tag_Code_Model].i));
2794
0
      result = false;
2795
0
    }
2796
2797
  /* The large code model is only supported by the MSP430X.  */
2798
0
  if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i == 2
2799
0
      && out_msp_attr[OFBA_MSPABI_Tag_ISA].i != 2)
2800
0
    {
2801
0
      _bfd_error_handler
2802
  /* xgettext:c-format */
2803
0
  (_("error: %pB uses the large code model but %pB uses MSP430 instructions"),
2804
0
   ibfd, first_input_bfd);
2805
0
      result = false;
2806
0
    }
2807
2808
  /* The data models must be the same.  */
2809
0
  if (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i
2810
0
      != out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i)
2811
0
    {
2812
0
      _bfd_error_handler
2813
  /* xgettext:c-format */
2814
0
  (_("error: %pB uses the %s data model whereas %pB uses the %s data model"),
2815
0
   ibfd, data_model (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i),
2816
0
   first_input_bfd,
2817
0
   data_model (out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i));
2818
0
      result = false;
2819
0
    }
2820
2821
  /* The small code model requires the use of the small data model.  */
2822
0
  if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i == 1
2823
0
      && out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i != 1)
2824
0
    {
2825
0
      _bfd_error_handler
2826
  /* xgettext:c-format */
2827
0
  (_("error: %pB uses the small code model but %pB uses the %s data model"),
2828
0
   ibfd, first_input_bfd,
2829
0
   data_model (out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i));
2830
0
      result = false;
2831
0
    }
2832
2833
  /* The large data models are only supported by the MSP430X.  */
2834
0
  if (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i > 1
2835
0
      && out_msp_attr[OFBA_MSPABI_Tag_ISA].i != 2)
2836
0
    {
2837
0
      _bfd_error_handler
2838
  /* xgettext:c-format */
2839
0
  (_("error: %pB uses the %s data model but %pB only uses MSP430 instructions"),
2840
0
   ibfd, data_model (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i),
2841
0
   first_input_bfd);
2842
0
      result = false;
2843
0
    }
2844
2845
  /* Just ignore the data region unless the large memory model is in use.
2846
     We have already checked that ibfd and obfd use the same memory model.  */
2847
0
  if ((in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i
2848
0
       == OFBA_MSPABI_Val_Code_Model_LARGE)
2849
0
      && (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i
2850
0
    == OFBA_MSPABI_Val_Data_Model_LARGE))
2851
0
    {
2852
      /* We cannot allow "lower region only" to be linked with any other
2853
   values (i.e. ANY or NONE).
2854
   Before this attribute existed, "ANY" region was the default.  */
2855
0
      bool ibfd_lower_region_used
2856
0
  = (in_gnu_attr[Tag_GNU_MSP430_Data_Region].i
2857
0
     == Val_GNU_MSP430_Data_Region_Lower);
2858
0
      bool obfd_lower_region_used
2859
0
  = (out_gnu_attr[Tag_GNU_MSP430_Data_Region].i
2860
0
     == Val_GNU_MSP430_Data_Region_Lower);
2861
0
      if (ibfd_lower_region_used != obfd_lower_region_used)
2862
0
  {
2863
0
    _bfd_error_handler
2864
0
      (_("error: %pB can use the upper region for data, "
2865
0
         "but %pB assumes data is exclusively in lower memory"),
2866
0
       ibfd_lower_region_used ? obfd : ibfd,
2867
0
       ibfd_lower_region_used ? ibfd : obfd);
2868
0
    result = false;
2869
0
  }
2870
0
    }
2871
2872
0
  return result;
2873
0
}
2874
2875
/* Merge backend specific data from an object file to the output
2876
   object file when linking.  */
2877
2878
static bool
2879
elf32_msp430_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2880
0
{
2881
0
  bfd *obfd = info->output_bfd;
2882
  /* Make sure that the machine number reflects the most
2883
     advanced version of the MSP architecture required.  */
2884
0
#define max(a,b) ((a) > (b) ? (a) : (b))
2885
0
  if (bfd_get_mach (ibfd) != bfd_get_mach (obfd))
2886
0
    bfd_default_set_arch_mach (obfd, bfd_get_arch (obfd),
2887
0
             max (bfd_get_mach (ibfd), bfd_get_mach (obfd)));
2888
0
#undef max
2889
2890
0
  return elf32_msp430_merge_msp430_attributes (ibfd, info);
2891
0
}
2892
2893
static bool
2894
msp430_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
2895
0
{
2896
0
  return _bfd_elf_is_local_label_name (abfd, sym->name);
2897
0
}
2898
2899
static bool
2900
uses_large_model (bfd *abfd)
2901
0
{
2902
0
  obj_attribute * attr;
2903
2904
0
  if (abfd->flags & BFD_LINKER_CREATED)
2905
0
    return false;
2906
2907
0
  attr = elf_known_obj_attributes_proc (abfd);
2908
0
  if (attr == NULL)
2909
0
    return false;
2910
2911
0
  return attr[OFBA_MSPABI_Tag_Code_Model].i == 2;
2912
0
}
2913
2914
static unsigned int
2915
elf32_msp430_eh_frame_address_size (bfd *abfd,
2916
            const asection *sec ATTRIBUTE_UNUSED)
2917
0
{
2918
0
  return uses_large_model (abfd) ? 4 : 2;
2919
0
}
2920
2921
/* This is gross.  The MSP430 EABI says that (sec 11.5):
2922
2923
     "An implementation may choose to use Rel or Rela
2924
      type relocations for other relocations."
2925
2926
   But it also says that:
2927
2928
     "Certain relocations are identified as Rela only. [snip]
2929
      Where Rela is specified, an implementation must honor
2930
      this requirement."
2931
2932
  There is one relocation marked as requiring RELA - R_MSP430_ABS_HI16 - but
2933
  to keep things simple we choose to use RELA relocations throughout.  The
2934
  problem is that the TI compiler generates REL relocations, so we have to
2935
  be able to accept those as well.  */
2936
2937
#define elf_backend_may_use_rel_p  1
2938
#define elf_backend_may_use_rela_p 1
2939
#define elf_backend_default_use_rela_p 1
2940
2941
#undef  elf_backend_obj_attrs_vendor
2942
#define elf_backend_obj_attrs_vendor    "mspabi"
2943
#undef  elf_backend_obj_attrs_section
2944
#define elf_backend_obj_attrs_section   ".MSP430.attributes"
2945
#undef  elf_backend_obj_attrs_section_type
2946
#define elf_backend_obj_attrs_section_type  SHT_MSP430_ATTRIBUTES
2947
#define elf_backend_section_from_shdr   elf32_msp430_section_from_shdr
2948
#define elf_backend_obj_attrs_handle_unknown  elf32_msp430_obj_attrs_handle_unknown
2949
#undef  elf_backend_obj_attrs_arg_type
2950
#define elf_backend_obj_attrs_arg_type    elf32_msp430_obj_attrs_arg_type
2951
#define bfd_elf32_bfd_merge_private_bfd_data  elf32_msp430_merge_private_bfd_data
2952
#define elf_backend_eh_frame_address_size elf32_msp430_eh_frame_address_size
2953
2954
#define ELF_ARCH    bfd_arch_msp430
2955
#define ELF_MACHINE_CODE  EM_MSP430
2956
#define ELF_MACHINE_ALT1  EM_MSP430_OLD
2957
#define ELF_MAXPAGESIZE   4
2958
#define ELF_OSABI   ELFOSABI_STANDALONE
2959
#define ELF_OSABI_EXACT   1
2960
2961
#define TARGET_LITTLE_SYM msp430_elf32_vec
2962
#define TARGET_LITTLE_NAME  "elf32-msp430"
2963
2964
#define elf_info_to_howto        msp430_info_to_howto_rela
2965
#define elf_info_to_howto_rel        NULL
2966
#define elf_backend_relocate_section       elf32_msp430_relocate_section
2967
#define elf_backend_check_relocs       elf32_msp430_check_relocs
2968
#define elf_backend_can_gc_sections      1
2969
#define elf_backend_final_write_processing   bfd_elf_msp430_final_write_processing
2970
#define elf_backend_object_p         elf32_msp430_object_p
2971
#define bfd_elf32_bfd_relax_section      msp430_elf_relax_section
2972
#define bfd_elf32_bfd_is_target_special_symbol  msp430_elf_is_target_special_symbol
2973
2974
#undef  elf32_bed
2975
#define elf32_bed   elf32_msp430_bed
2976
2977
#include "elf32-target.h"
2978
2979
/* The TI compiler sets the OSABI field to ELFOSABI_NONE.  */
2980
#undef  TARGET_LITTLE_SYM
2981
#define TARGET_LITTLE_SYM msp430_elf32_ti_vec
2982
2983
#undef  elf32_bed
2984
#define elf32_bed   elf32_msp430_ti_bed
2985
2986
#undef  ELF_OSABI
2987
#undef  ELF_OSABI_EXACT
2988
2989
static const struct bfd_elf_special_section msp430_ti_elf_special_sections[] =
2990
{
2991
  /* prefix, prefix_length,    suffix_len, type,       attributes.  */
2992
  { STRING_COMMA_LEN (".TI.symbol.alias"),  0, SHT_MSP430_SYM_ALIASES, 0 },
2993
  { STRING_COMMA_LEN (".TI.section.flags"), 0, SHT_MSP430_SEC_FLAGS,   0 },
2994
  { STRING_COMMA_LEN ("_TI_build_attrib"),  0, SHT_MSP430_ATTRIBUTES,  0 },
2995
  { NULL, 0,            0, 0,          0 }
2996
};
2997
2998
#undef  elf_backend_special_sections
2999
#define elf_backend_special_sections    msp430_ti_elf_special_sections
3000
3001
#include "elf32-target.h"