Coverage Report

Created: 2026-11-21 09:00

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