1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/*
* PA-RISC specific support for Elf loading and relocation.
* Copyright 2000 David Huggins-Daines <dhd@linuxcare.com>, Linuxcare Inc.
* Copyright 2000 Richard Hirst <rhirst@linuxcare.com>, Linuxcare Inc.
*
* Based on the IA-64 support, which is:
* Copyright 2000 Mike Stephens <mike.stephens@intel.com>
*
* This file is part of the Linux modutils.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <module.h>
#include <obj.h>
#include <util.h>
#include <modstat.h> /* For ksyms */
typedef struct _hppa_stub_t
{
struct _hppa_stub_t *next;
int offset;
int reloc_done;
} hppa_stub_t;
typedef struct _hppa_symbol_t
{
struct obj_symbol root;
hppa_stub_t *stub;
} hppa_symbol_t;
typedef struct _hppa_file_t
{
struct obj_file root;
struct obj_section *stub;
Elf32_Addr dp;
} hppa_file_t;
/* The ABI defines various more esoteric types, but these are the only
ones we actually need. */
enum hppa_fsel
{
e_fsel,
e_lsel,
e_rsel,
e_lrsel,
e_rrsel
};
struct obj_file *
arch_new_file (void)
{
hppa_file_t *f;
f = xmalloc(sizeof(*f));
f->stub = NULL;
return &f->root;
}
struct obj_section *
arch_new_section (void)
{
return xmalloc(sizeof(struct obj_section));
}
struct obj_symbol *
arch_new_symbol (void)
{
hppa_symbol_t *sym;
sym = xmalloc(sizeof(*sym));
sym->stub = NULL;
return &sym->root;
}
/* This is called for architecture specific sections we might need to
do special things to. */
int
arch_load_proc_section(struct obj_section *sec, int fp)
{
/* Assume it's just a debugging section that we can safely
ignore ... */
sec->contents = NULL;
return 0;
}
/* =================================================================
These functions are from libhppa.h in the GNU BFD library.
(c) 1990, 91, 92, 93, 94, 95, 96, 98, 99, 2000
Free Software Foundation, Inc.
================================================================= */
/* The *sign_extend functions are used to assemble various bitfields
taken from an instruction and return the resulting immediate
value. */
static inline int
sign_extend (x, len)
int x, len;
{
int signbit = (1 << (len - 1));
int mask = (signbit << 1) - 1;
return ((x & mask) ^ signbit) - signbit;
}
static inline int
low_sign_extend (x, len)
int x, len;
{
return (x >> 1) - ((x & 1) << (len - 1));
}
/* The re_assemble_* functions prepare an immediate value for
insertion into an opcode. pa-risc uses all sorts of weird bitfields
in the instruction to hold the value. */
static inline int
sign_unext (x, len)
int x, len;
{
int len_ones;
len_ones = (1 << len) - 1;
return x & len_ones;
}
static inline int
low_sign_unext (x, len)
int x, len;
{
int temp;
int sign;
sign = (x >> (len-1)) & 1;
temp = sign_unext (x, len-1);
return (temp << 1) | sign;
}
static inline int
re_assemble_3 (as3)
int as3;
{
return (( (as3 & 4) << (13-2))
| ((as3 & 3) << (13+1)));
}
static inline int
re_assemble_12 (as12)
int as12;
{
return (( (as12 & 0x800) >> 11)
| ((as12 & 0x400) >> (10 - 2))
| ((as12 & 0x3ff) << (1 + 2)));
}
static inline int
re_assemble_14 (as14)
int as14;
{
return (( (as14 & 0x1fff) << 1)
| ((as14 & 0x2000) >> 13));
}
static inline int
re_assemble_16 (as16)
int as16;
{
int s, t;
/* Unusual 16-bit encoding, for wide mode only. */
t = (as16 << 1) & 0xffff;
s = (as16 & 0x8000);
return (t ^ s ^ (s >> 1)) | (s >> 15);
}
static inline int
re_assemble_17 (as17)
int as17;
{
return (( (as17 & 0x10000) >> 16)
| ((as17 & 0x0f800) << (16 - 11))
| ((as17 & 0x00400) >> (10 - 2))
| ((as17 & 0x003ff) << (1 + 2)));
}
static inline int
re_assemble_21 (as21)
int as21;
{
return (( (as21 & 0x100000) >> 20)
| ((as21 & 0x0ffe00) >> 8)
| ((as21 & 0x000180) << 7)
| ((as21 & 0x00007c) << 14)
| ((as21 & 0x000003) << 12));
}
static inline int
re_assemble_22 (as22)
int as22;
{
return (( (as22 & 0x200000) >> 21)
| ((as22 & 0x1f0000) << (21 - 16))
| ((as22 & 0x00f800) << (16 - 11))
| ((as22 & 0x000400) >> (10 - 2))
| ((as22 & 0x0003ff) << (1 + 2)));
}
/* Handle field selectors for PA instructions.
The L and R (and LS, RS etc.) selectors are used in pairs to form a
full 32 bit address. eg.
LDIL L'start,%r1 ; put left part into r1
LDW R'start(%r1),%r2 ; add r1 and right part to form address
This function returns sign extended values in all cases.
*/
static inline unsigned int
hppa_field_adjust (value, addend, r_field)
unsigned int value;
int addend;
enum hppa_fsel r_field;
{
unsigned int sym_val;
sym_val = value - addend;
switch (r_field)
{
case e_fsel:
/* F: No change. */
break;
case e_lsel:
/* L: Select top 21 bits. */
value = value >> 11;
break;
case e_rsel:
/* R: Select bottom 11 bits. */
value = value & 0x7ff;
break;
case e_lrsel:
/* LR: L with rounding of the addend to nearest 8k. */
value = sym_val + ((addend + 0x1000) & -0x2000);
value = value >> 11;
break;
case e_rrsel:
/* RR: R with rounding of the addend to nearest 8k.
We need to return a value such that 2048 * LR'x + RR'x == x
ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
. = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
. = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000) */
value = (sym_val & 0x7ff) + (((addend & 0x1fff) ^ 0x1000) - 0x1000);
break;
default:
abort();
}
return value;
}
/* Insert VALUE into INSN using R_FORMAT to determine exactly what
bits to change. */
static inline int
hppa_rebuild_insn (insn, value, r_format)
int insn;
int value;
int r_format;
{
switch (r_format)
{
case 11:
return (insn & ~ 0x7ff) | low_sign_unext (value, 11);
case 12:
return (insn & ~ 0x1ffd) | re_assemble_12 (value);
case 10:
return (insn & ~ 0x3ff1) | re_assemble_14 (value & -8);
case -11:
return (insn & ~ 0x3ff9) | re_assemble_14 (value & -4);
case 14:
return (insn & ~ 0x3fff) | re_assemble_14 (value);
case -10:
return (insn & ~ 0xfff1) | re_assemble_16 (value & -8);
case -16:
return (insn & ~ 0xfff9) | re_assemble_16 (value & -4);
case 16:
return (insn & ~ 0xffff) | re_assemble_16 (value);
case 17:
return (insn & ~ 0x1f1ffd) | re_assemble_17 (value);
case 21:
return (insn & ~ 0x1fffff) | re_assemble_21 (value);
case 22:
return (insn & ~ 0x3ff1ffd) | re_assemble_22 (value);
case 32:
return value;
default:
abort ();
}
return insn;
}
/* ====================================================================
End of functions from GNU BFD.
==================================================================== */
/* This is where we get the opportunity to create any extra dynamic
sections we might need. In our case we do not need a GOT because
our code is not PIC, but we do need to create a stub section.
This is significantly less complex than what we do for shared
libraries because, obviously, modules are not shared. Also we have
no issues related to symbol visibility, lazy linking, etc.
The kernels dp is fixed (at symbol $global$), and we can fix up any
DPREL refs in the module to use that same dp value.
All PCREL* refs result in a stub with the following format:
ldil L'func_addr,%r1
be,n R'func_addr(%sr4,%r1)
Note, all PCREL* get a stub, regardless of whether they are
local or external. With local ones, and external ones to other
modules, there is a good chance we could manage without the stub.
I'll leave that for a future optimisation.
*/
#define LDIL_R1 0x20200000 /* ldil L'XXX,%r1 */
#define BE_N_SR4_R1 0xe0202002 /* be,n R'XXX(%sr4,%r1) */
#define STUB_SIZE 8
int
arch_create_got(struct obj_file *f)
{
hppa_file_t *hfile = (hppa_file_t *)f;
int i, n;
int stub_offset = 0;
/* Create stub section.
* XXX set flags, see obj_ia64.c
*/
hfile->stub = obj_create_alloced_section(f, ".stub", STUB_SIZE,
0, SHF_WRITE);
/* Actually this is a lot like check_relocs() in a BFD backend. We
walk all sections and all their relocations and look for ones
that need special treatment. */
n = hfile->root.header.e_shnum;
for (i = 0; i < n; ++i)
{
struct obj_section *relsec, *symsec, *strsec;
Elf32_Rela *rel, *relend;
Elf32_Sym *symtab;
char const *strtab;
relsec = hfile->root.sections[i];
if (relsec->header.sh_type != SHT_RELA)
continue;
symsec = hfile->root.sections[relsec->header.sh_link];
strsec = hfile->root.sections[symsec->header.sh_link];
rel = (Elf32_Rela *)relsec->contents;
relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
symtab = (Elf32_Sym *)symsec->contents;
strtab = (char const *)strsec->contents;
for (; rel < relend; rel++)
{
int need_stub = 0;
switch (ELF32_R_TYPE(rel->r_info))
{
default:
continue;
case R_PARISC_PCREL17F:
case R_PARISC_PCREL22F:
need_stub = 1;
break;
}
if (need_stub)
{
hppa_symbol_t *hsym;
int local;
obj_find_relsym(hsym, f, f, rel, symtab, strtab);
local = hsym->root.secidx <= SHN_HIRESERVE;
if (need_stub)
{
hppa_stub_t *stub;
if (hsym->stub == NULL)
{
stub = (hppa_stub_t *) xmalloc(sizeof(hppa_stub_t));
stub->offset = stub_offset;
stub->reloc_done = 0;
hsym->stub = stub;
stub_offset += STUB_SIZE;
need_stub = 0;
}
}
}
}
}
if (stub_offset)
{
hfile->stub->contents = xmalloc(stub_offset);
hfile->stub->header.sh_size = stub_offset;
}
return 1;
}
enum obj_reloc
arch_apply_relocation(struct obj_file *f,
struct obj_section *targsec,
struct obj_section *symsec,
struct obj_symbol *sym,
Elf32_Rela *rel,
Elf32_Addr v)
{
hppa_file_t *hfile = (hppa_file_t *) f;
hppa_symbol_t *hsym = (hppa_symbol_t *) sym;
Elf32_Addr *loc = (Elf32_Addr *)(targsec->contents + rel->r_offset);
Elf32_Addr dot = (targsec->header.sh_addr + rel->r_offset) & ~0x03;
Elf32_Addr dp = hfile->dp;
Elf32_Word r_info = ELF32_R_TYPE(rel->r_info);
enum obj_reloc ret = obj_reloc_ok;
enum hppa_fsel fsel = e_fsel; /* Avoid compiler warning */
unsigned int r_format;
/* Fix up the value, and determine whether we can handle this
relocation. */
switch (r_info)
{
case R_PARISC_PLABEL32:
case R_PARISC_DIR32:
case R_PARISC_DIR21L:
case R_PARISC_DIR14R:
/* Easy. */
break;
case R_PARISC_SEGREL32:
v -= f->baseaddr;
break;
case R_PARISC_DPREL21L:
case R_PARISC_DPREL14R:
v -= dp;
break;
case R_PARISC_PCREL17F:
case R_PARISC_PCREL22F:
/* Find an import stub. */
assert(hsym->stub != NULL);
assert(hfile->stub != NULL);
/* XXX Optimise. We may not need a stub for short branches */
if (!hsym->stub->reloc_done) {
/* Need to create the .stub entry */
Elf32_Addr *pstub, stubv;
pstub = (Elf32_Addr *)(hfile->stub->contents + hsym->stub->offset);
pstub[0] = LDIL_R1;
pstub[1] = BE_N_SR4_R1;
stubv = hppa_field_adjust(v, rel->r_addend, e_lrsel);
pstub[0] = hppa_rebuild_insn(pstub[0], stubv, 21);
stubv = hppa_field_adjust(v, rel->r_addend, e_rrsel);
stubv >>= 2; /* Branch; divide by 4 */
pstub[1] = hppa_rebuild_insn(pstub[1], stubv, 17);
hsym->stub->reloc_done = 1;
}
v = hsym->stub->offset + hfile->stub->header.sh_addr;
break;
default:
return obj_reloc_unhandled;
}
/* Find the field selector. */
switch (r_info)
{
case R_PARISC_DIR32:
case R_PARISC_PLABEL32:
case R_PARISC_PCREL17F:
case R_PARISC_SEGREL32:
case R_PARISC_PCREL22F:
fsel = e_fsel;
break;
case R_PARISC_DPREL21L:
case R_PARISC_DIR21L:
fsel = e_lrsel;
break;
case R_PARISC_DPREL14R:
case R_PARISC_DIR14R:
fsel = e_rrsel;
break;
}
v = hppa_field_adjust(v, rel->r_addend, fsel);
switch (r_info)
{
case R_PARISC_PCREL17F:
case R_PARISC_PCREL17R:
case R_PARISC_PCREL22F:
v = v - dot - 8;
case R_PARISC_DIR17F:
case R_PARISC_DIR17R:
/* This is a branch. Divide the offset by four. */
v >>= 2;
break;
default:
break;
}
/* Find the format. */
switch (r_info)
{
case R_PARISC_DIR32:
case R_PARISC_PLABEL32:
case R_PARISC_SEGREL32:
r_format = 32;
break;
case R_PARISC_DPREL21L:
case R_PARISC_DIR21L:
r_format = 21;
break;
case R_PARISC_PCREL17F:
r_format = 17;
break;
case R_PARISC_DPREL14R:
case R_PARISC_DIR14R:
r_format = 14;
break;
case R_PARISC_PCREL22F:
r_format = 22;
break;
default:
abort();
}
*loc = hppa_rebuild_insn(*loc, v, r_format);
return ret;
}
int
arch_init_module (struct obj_file *f, struct module *mod)
{
return 1;
}
int
arch_finalize_section_address(struct obj_file *f, Elf32_Addr base)
{
int i, n = f->header.e_shnum;
f->baseaddr = base;
for (i = 0; i < n; ++i)
f->sections[i]->header.sh_addr += base;
return 1;
}
int
arch_archdata (struct obj_file *f, struct obj_section *sec)
{
struct module_symbol *s;
int i;
hppa_file_t *hfile = (hppa_file_t *)f;
/* Initialise dp to the kernels dp (symbol $global$)
*/
for (i = 0, s = ksyms; i < nksyms; i++, s++)
if (!strcmp((char *)s->name, "$global$"))
break;
if (i >= nksyms) {
error("Cannot initialise dp, '$global$' not found\n");
return 1;
}
hfile->dp = s->value;
return 0;
}
|