ELFObject.hxx revision 2a7f249060179fdcb49f240bdf349c4e7b896084
1/* 2 * Copyright 2011, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ELF_OBJECT_HXX 18#define ELF_OBJECT_HXX 19 20#include "ELFHeader.h" 21#include "ELFReloc.h" 22#include "ELFSection.h" 23#include "ELFSectionHeaderTable.h" 24#include "StubLayout.h" 25#include "GOT.h" 26#include "ELF.h" 27 28#include <llvm/ADT/SmallVector.h> 29 30#include "utils/rsl_assert.h" 31 32template <unsigned Bitwidth> 33template <typename Archiver> 34inline ELFObject<Bitwidth> * 35ELFObject<Bitwidth>::read(Archiver &AR) { 36 llvm::OwningPtr<ELFObjectTy> object(new ELFObjectTy()); 37 38 // Read header 39 object->header.reset(ELFHeaderTy::read(AR)); 40 if (!object->header) { 41 return 0; 42 } 43 44 // Read section table 45 object->shtab.reset(ELFSectionHeaderTableTy::read(AR, object.get())); 46 if (!object->shtab) { 47 return 0; 48 } 49 50 // Read each section 51 llvm::SmallVector<size_t, 4> progbits_ndx; 52 for (size_t i = 0; i < object->header->getSectionHeaderNum(); ++i) { 53 if ((*object->shtab)[i]->getType() == SHT_PROGBITS) { 54 object->stab.push_back(NULL); 55 progbits_ndx.push_back(i); 56 } else { 57 llvm::OwningPtr<ELFSectionTy> sec( 58 ELFSectionTy::read(AR, object.get(), (*object->shtab)[i])); 59 object->stab.push_back(sec.take()); 60 } 61 } 62 63 object->shtab->buildNameMap(); 64 ELFSectionSymTabTy *symtab = 65 static_cast<ELFSectionSymTabTy *>(object->getSectionByName(".symtab")); 66 rsl_assert(symtab && "Symtab is required."); 67 symtab->buildNameMap(); 68 69 for (size_t i = 0; i < progbits_ndx.size(); ++i) { 70 size_t index = progbits_ndx[i]; 71 72 llvm::OwningPtr<ELFSectionTy> sec( 73 ELFSectionTy::read(AR, object.get(), (*object->shtab)[index])); 74 object->stab[index] = sec.take(); 75 } 76 77 return object.take(); 78} 79 80template <unsigned Bitwidth> 81inline char const *ELFObject<Bitwidth>::getSectionName(size_t i) const { 82 ELFSectionTy const *sec = stab[header->getStringSectionIndex()]; 83 84 if (sec) { 85 ELFSectionStrTabTy const &st = 86 static_cast<ELFSectionStrTabTy const &>(*sec); 87 return st[i]; 88 } 89 90 return NULL; 91} 92 93template <unsigned Bitwidth> 94inline ELFSection<Bitwidth> const * 95ELFObject<Bitwidth>::getSectionByIndex(size_t i) const { 96 return stab[i]; 97} 98 99template <unsigned Bitwidth> 100inline ELFSection<Bitwidth> * 101ELFObject<Bitwidth>::getSectionByIndex(size_t i) { 102 return stab[i]; 103} 104 105template <unsigned Bitwidth> 106inline ELFSection<Bitwidth> const * 107ELFObject<Bitwidth>::getSectionByName(std::string const &str) const { 108 size_t idx = getSectionHeaderTable()->getByName(str)->getIndex(); 109 return stab[idx]; 110} 111 112template <unsigned Bitwidth> 113inline ELFSection<Bitwidth> * 114ELFObject<Bitwidth>::getSectionByName(std::string const &str) { 115 ELFObjectTy const *const_this = this; 116 ELFSectionTy const *sptr = const_this->getSectionByName(str); 117 // Const cast for the same API's const and non-const versions. 118 return const_cast<ELFSectionTy *>(sptr); 119} 120 121 122template <unsigned Bitwidth> 123inline void ELFObject<Bitwidth>:: 124relocateARM(void *(*find_sym)(void *context, char const *name), 125 void *context, 126 ELFSectionRelTableTy *reltab, 127 ELFSectionProgBitsTy *text) { 128 // FIXME: Should be implement in independent files. 129 rsl_assert(Bitwidth == 32 && "ARM only have 32 bits."); 130 131 ELFSectionSymTabTy *symtab = 132 static_cast<ELFSectionSymTabTy *>(getSectionByName(".symtab")); 133 rsl_assert(symtab && "Symtab is required."); 134 135 for (size_t i = 0; i < reltab->size(); ++i) { 136 // FIXME: Can not implement here, use Fixup! 137 ELFRelocTy *rel = (*reltab)[i]; 138 ELFSymbolTy *sym = (*symtab)[rel->getSymTabIndex()]; 139 140 // FIXME: May be not uint32_t *. 141 typedef int32_t Inst_t; 142 Inst_t *inst = (Inst_t *)&(*text)[rel->getOffset()]; 143 Inst_t P = (Inst_t)(int64_t)inst; 144 Inst_t A = 0; 145 Inst_t S = (Inst_t)(int64_t)sym->getAddress(EM_ARM); 146 Inst_t T = 0; 147 148 if (sym->isConcreteFunc() && (sym->getValue() & 0x1)) { 149 T = 1; 150 } 151 152 switch (rel->getType()) { 153 default: 154 rsl_assert(0 && "Not implemented relocation type."); 155 break; 156 157 // Unknown to me. 158 case R_ARM_NONE: 159 { 160 *inst = S|T; 161 } 162 163 case R_ARM_ABS32: 164 { 165 A = *inst; 166 *inst = (S + A) | T; 167 } 168 break; 169 170 // FIXME: Predefine relocation codes. 171 case R_ARM_CALL: 172 case R_ARM_THM_CALL: 173 { 174#define SIGN_EXTEND(x, l) (((x)^(1<<((l)-1)))-(1<<(l-1))) 175 if (rel->getType() == R_ARM_CALL) { 176 A = (Inst_t)(int64_t)SIGN_EXTEND(*inst & 0xFFFFFF, 24); 177 A <<= 2; 178 } 179 else { 180 // Hack for two 16bit. 181 *inst = ((*inst >> 16) & 0xFFFF) | (*inst << 16); 182 Inst_t s = (*inst >> 26) & 0x1u, // 26 183 u = (*inst >> 16) & 0x3FFu, // 25-16 184 l = *inst & 0x7FFu, // 10-0 185 j1 = (*inst >> 13) & 0x1u, // 13 186 j2 = (*inst >> 11) & 0x1u; // 11 187 Inst_t i1 = (~(j1 ^ s)) & 0x1u, 188 i2 = (~(j2 ^ s)) & 0x1u; 189 // [31-25][24][23][22][21-12][11-1][0] 190 // 0 s i1 i2 u l 0 191 A = SIGN_EXTEND((s << 23) | (i1 << 22) | (i2 << 21) | (u << 11) | l, 24); 192 A <<= 1; 193 } 194#undef SIGN_EXTEND 195 196 void *callee_addr = sym->getAddress(EM_ARM); 197 198 switch (sym->getType()) { 199 default: 200 rsl_assert(0 && "Wrong type for R_ARM_CALL relocation."); 201 abort(); 202 break; 203 204 case STT_FUNC: 205 // NOTE: Callee function is in the object file, but it may be 206 // in different PROGBITS section (which may be far call). 207 208 if (callee_addr == 0) { 209 rsl_assert(0 && "We should get function address at previous " 210 "sym->getAddress(EM_ARM) function call."); 211 abort(); 212 } 213 break; 214 215 case STT_NOTYPE: 216 // NOTE: Callee function is an external function. Call find_sym 217 // if it has not resolved yet. 218 219 if (callee_addr == 0) { 220 callee_addr = find_sym(context, sym->getName()); 221 if (!callee_addr) { 222 missingSymbols = true; 223 } 224 sym->setAddress(callee_addr); 225 } 226 break; 227 } 228 229 // Get the stub for this function 230 StubLayout *stub_layout = text->getStubLayout(); 231 232 if (!stub_layout) { 233 llvm::errs() << "unable to get stub layout." << "\n"; 234 abort(); 235 } 236 237 void *stub = stub_layout->allocateStub(callee_addr); 238 239 if (!stub) { 240 llvm::errs() << "unable to allocate stub." << "\n"; 241 abort(); 242 } 243 244 //LOGI("Function %s: using stub %p\n", sym->getName(), stub); 245 S = (uint32_t)(uintptr_t)stub; 246 247 if (rel->getType() == R_ARM_CALL) { 248 // Relocate the R_ARM_CALL relocation type 249 uint32_t result = (S + A - P) >> 2; 250 251 if (result > 0x007FFFFF && result < 0xFF800000) { 252 rsl_assert(0 && "Stub is still too far"); 253 abort(); 254 } 255 256 *inst = ((result) & 0x00FFFFFF) | (*inst & 0xFF000000); 257 } 258 else { 259 // Relocate the R_ARM_THM_CALL relocation type 260 uint32_t result = (S + A - P) >> 1; 261 262 if (result > 0x007FFFFF && result < 0xFF800000) { 263 rsl_assert(0 && "Stub is still too far"); 264 abort(); 265 } 266 267 // Rewrite instruction to BLX. 268 *inst &= 0xF800C000u; 269 // [31-25][24][23][22][21-12][11-1][0] 270 // 0 s i1 i2 u l 0 271 Inst_t s = (result >> 23) & 0x1u, // 26 272 u = (result >> 11) & 0x3FFu, // 25-16 273 // For BLX, bit [0] is 0. 274 l = result & 0x7FEu, // 10-0 275 i1 = (result >> 22) & 0x1u, 276 i2 = (result >> 21) & 0x1u; 277 Inst_t j1 = ((~i1) ^ s) & 0x01u, // 13 278 j2 = ((~i2) ^ s) & 0x01u; // 11 279 *inst |= s << 26; 280 *inst |= u << 16; 281 *inst |= l; 282 *inst |= j1 << 13; 283 *inst |= j2 << 11; 284 // Hack for two 16bit. 285 *inst = ((*inst >> 16) & 0xFFFF) | (*inst << 16); 286 } 287 } 288 break; 289 case R_ARM_MOVT_ABS: 290 case R_ARM_MOVW_ABS_NC: 291 case R_ARM_THM_MOVW_ABS_NC: 292 case R_ARM_THM_MOVT_ABS: 293 { 294 if (S==0 && sym->getType() == STT_NOTYPE) 295 { 296 void *ext_sym = find_sym(context, sym->getName()); 297 if (!ext_sym) { 298 missingSymbols = true; 299 } 300 S = (Inst_t)(uintptr_t)ext_sym; 301 sym->setAddress(ext_sym); 302 } 303 if (rel->getType() == R_ARM_MOVT_ABS 304 || rel->getType() == R_ARM_THM_MOVT_ABS) { 305 S >>= 16; 306 } 307 308 if (rel->getType() == R_ARM_MOVT_ABS 309 || rel->getType() == R_ARM_MOVW_ABS_NC) { 310 // No need sign extend. 311 A = ((*inst & 0xF0000) >> 4) | (*inst & 0xFFF); 312 uint32_t result = (S + A); 313 *inst = (((result) & 0xF000) << 4) | 314 ((result) & 0xFFF) | 315 (*inst & 0xFFF0F000); 316 } 317 else { 318 // Hack for two 16bit. 319 *inst = ((*inst >> 16) & 0xFFFF) | (*inst << 16); 320 // imm16: [19-16][26][14-12][7-0] 321 A = (((*inst >> 4) & 0xF000u) | 322 ((*inst >> 15) & 0x0800u) | 323 ((*inst >> 4) & 0x0700u) | 324 ( *inst & 0x00FFu)); 325 uint32_t result; 326 if (rel->getType() == R_ARM_THM_MOVT_ABS) { 327 result = (S + A); 328 } else { 329 result = (S + A) | T; 330 } 331 // imm16: [19-16][26][14-12][7-0] 332 *inst &= 0xFBF08F00u; 333 *inst |= (result & 0xF000u) << 4; 334 *inst |= (result & 0x0800u) << 15; 335 *inst |= (result & 0x0700u) << 4; 336 *inst |= (result & 0x00FFu); 337 // Hack for two 16bit. 338 *inst = ((*inst >> 16) & 0xFFFF) | (*inst << 16); 339 } 340 } 341 break; 342 } 343 //llvm::errs() << "S: " << (void *)S << '\n'; 344 //llvm::errs() << "A: " << (void *)A << '\n'; 345 //llvm::errs() << "P: " << (void *)P << '\n'; 346 //llvm::errs() << "S+A: " << (void *)(S+A) << '\n'; 347 //llvm::errs() << "S+A-P: " << (void *)(S+A-P) << '\n'; 348 } 349} 350 351template <unsigned Bitwidth> 352inline void ELFObject<Bitwidth>:: 353relocateX86_64(void *(*find_sym)(void *context, char const *name), 354 void *context, 355 ELFSectionRelTableTy *reltab, 356 ELFSectionProgBitsTy *text) { 357 rsl_assert(Bitwidth == 64 && "Only support X86_64."); 358 359 ELFSectionSymTabTy *symtab = 360 static_cast<ELFSectionSymTabTy *>(getSectionByName(".symtab")); 361 rsl_assert(symtab && "Symtab is required."); 362 363 for (size_t i = 0; i < reltab->size(); ++i) { 364 // FIXME: Can not implement here, use Fixup! 365 ELFRelocTy *rel = (*reltab)[i]; 366 ELFSymbolTy *sym = (*symtab)[rel->getSymTabIndex()]; 367 368 //typedef uint64_t Inst_t; 369 typedef int32_t Inst_t; 370 Inst_t *inst = (Inst_t *)&(*text)[rel->getOffset()]; 371 Inst_t P = (Inst_t)(int64_t)inst; 372 Inst_t A = (Inst_t)(int64_t)rel->getAddend(); 373 Inst_t S = (Inst_t)(int64_t)sym->getAddress(EM_X86_64); 374 375 if (S == 0) { 376 S = (Inst_t)(int64_t)find_sym(context, sym->getName()); 377 if (!S) { 378 missingSymbols = true; 379 } 380 sym->setAddress((void *)S); 381 } 382 383 switch (rel->getType()) { 384 default: 385 rsl_assert(0 && "Not implemented relocation type."); 386 break; 387 388 // FIXME: XXX: R_X86_64_64 is 64 bit, there is a big problem here. 389 case 1: // R_X86_64_64 390 *inst = (S+A); 391 break; 392 393 case 2: // R_X86_64_PC32 394 *inst = (S+A-P); 395 break; 396 397 case 10: // R_X86_64_32 398 case 11: // R_X86_64_32S 399 *inst = (S+A); 400 break; 401 } 402 } 403} 404 405template <unsigned Bitwidth> 406inline void ELFObject<Bitwidth>:: 407relocateX86_32(void *(*find_sym)(void *context, char const *name), 408 void *context, 409 ELFSectionRelTableTy *reltab, 410 ELFSectionProgBitsTy *text) { 411 rsl_assert(Bitwidth == 32 && "Only support X86."); 412 413 ELFSectionSymTabTy *symtab = 414 static_cast<ELFSectionSymTabTy *>(getSectionByName(".symtab")); 415 rsl_assert(symtab && "Symtab is required."); 416 417 for (size_t i = 0; i < reltab->size(); ++i) { 418 // FIXME: Can not implement here, use Fixup! 419 ELFRelocTy *rel = (*reltab)[i]; 420 ELFSymbolTy *sym = (*symtab)[rel->getSymTabIndex()]; 421 422 //typedef uint64_t Inst_t; 423 typedef int32_t Inst_t; 424 Inst_t *inst = (Inst_t *)&(*text)[rel->getOffset()]; 425 Inst_t P = (Inst_t)(uintptr_t)inst; 426 Inst_t A = (Inst_t)(uintptr_t)*inst; 427 Inst_t S = (Inst_t)(uintptr_t)sym->getAddress(EM_386); 428 429 if (S == 0) { 430 S = (Inst_t)(uintptr_t)find_sym(context, sym->getName()); 431 if (!S) { 432 missingSymbols = true; 433 } 434 sym->setAddress((void *)S); 435 } 436 437 switch (rel->getType()) { 438 default: 439 rsl_assert(0 && "Not implemented relocation type."); 440 break; 441 442 case R_386_PC32: 443 *inst = (S+A-P); 444 break; 445 446 case R_386_32: 447 *inst = (S+A); 448 break; 449 } 450 } 451} 452 453template <unsigned Bitwidth> 454inline void ELFObject<Bitwidth>:: 455relocateMIPS(void *(*find_sym)(void *context, char const *name), 456 void *context, 457 ELFSectionRelTableTy *reltab, 458 ELFSectionProgBitsTy *text) { 459 rsl_assert(Bitwidth == 32 && "Only support 32-bit MIPS."); 460 461 ELFSectionSymTabTy *symtab = 462 static_cast<ELFSectionSymTabTy *>(getSectionByName(".symtab")); 463 rsl_assert(symtab && "Symtab is required."); 464 465 for (size_t i = 0; i < reltab->size(); ++i) { 466 // FIXME: Can not implement here, use Fixup! 467 ELFRelocTy *rel = (*reltab)[i]; 468 ELFSymbolTy *sym = (*symtab)[rel->getSymTabIndex()]; 469 470 typedef int32_t Inst_t; 471 Inst_t *inst = (Inst_t *)&(*text)[rel->getOffset()]; 472 Inst_t P = (Inst_t)(uintptr_t)inst; 473 Inst_t A = (Inst_t)(uintptr_t)*inst; 474 Inst_t S = (Inst_t)(uintptr_t)sym->getAddress(EM_MIPS); 475 476 bool need_stub = false; 477 478 if (S == 0 && strcmp (sym->getName(), "_gp_disp") != 0) { 479 need_stub = true; 480 S = (Inst_t)(uintptr_t)find_sym(context, sym->getName()); 481 if (!S) { 482 missingSymbols = true; 483 } 484 sym->setAddress((void *)S); 485 } 486 487 switch (rel->getType()) { 488 default: 489 rsl_assert(0 && "Not implemented relocation type."); 490 break; 491 492 case R_MIPS_NONE: 493 case R_MIPS_JALR: // ignore this 494 break; 495 496 case R_MIPS_16: 497 *inst &= 0xFFFF0000; 498 A = A & 0xFFFF; 499 A = S + (short)A; 500 rsl_assert(A >= -32768 && A <= 32767 && "R_MIPS_16 overflow."); 501 *inst |= (A & 0xFFFF); 502 break; 503 504 case R_MIPS_32: 505 *inst = S + A; 506 break; 507 508 case R_MIPS_26: 509 *inst &= 0xFC000000; 510 if (need_stub == false) { 511 A = (A & 0x3FFFFFF) << 2; 512 if (sym->getBindingAttribute() == STB_LOCAL) { // local binding 513 A |= ((P + 4) & 0xF0000000); 514 A += S; 515 *inst |= ((A >> 2) & 0x3FFFFFF); 516 } 517 else { // external binding 518 if (A & 0x08000000) // Sign extend from bit 27 519 A |= 0xF0000000; 520 A += S; 521 *inst |= ((A >> 2) & 0x3FFFFFF); 522 if (((P + 4) >> 28) != (A >> 28)) { // far local call 523 void *stub = text->getStubLayout()->allocateStub((void *)A); 524 rsl_assert(stub && "cannot allocate stub."); 525 sym->setAddress(stub); 526 S = (int32_t)(intptr_t)stub; 527 *inst |= ((S >> 2) & 0x3FFFFFF); 528 rsl_assert(((P + 4) >> 28) == (S >> 28) && "stub is too far."); 529 } 530 } 531 } 532 else { // shared-library call 533 A = (A & 0x3FFFFFF) << 2; 534 rsl_assert(A == 0 && "R_MIPS_26 addend is not zero."); 535 void *stub = text->getStubLayout()->allocateStub((void *)S); 536 rsl_assert(stub && "cannot allocate stub."); 537 sym->setAddress(stub); 538 S = (int32_t)(intptr_t)stub; 539 *inst |= ((S >> 2) & 0x3FFFFFF); 540 rsl_assert(((P + 4) >> 28) == (S >> 28) && "stub is too far."); 541 } 542 break; 543 544 case R_MIPS_HI16: 545 *inst &= 0xFFFF0000; 546 A = (A & 0xFFFF) << 16; 547 // Find the nearest LO16 relocation type after this entry 548 for (size_t j = i + 1; j < reltab->size(); j++) { 549 ELFRelocTy *this_rel = (*reltab)[j]; 550 ELFSymbolTy *this_sym = (*symtab)[this_rel->getSymTabIndex()]; 551 if (this_rel->getType() == R_MIPS_LO16 && this_sym == sym) { 552 Inst_t *this_inst = (Inst_t *)&(*text)[this_rel->getOffset()]; 553 Inst_t this_A = (Inst_t)(uintptr_t)*this_inst; 554 this_A = this_A & 0xFFFF; 555 A += (short)this_A; 556 break; 557 } 558 } 559 if (strcmp (sym->getName(), "_gp_disp") == 0) { 560 S = (int)(intptr_t)got_address() + GP_OFFSET - (int)P; 561 sym->setAddress((void *)S); 562 } 563 *inst |= (((S + A + (int)0x8000) >> 16) & 0xFFFF); 564 break; 565 566 case R_MIPS_LO16: 567 *inst &= 0xFFFF0000; 568 A = A & 0xFFFF; 569 if (strcmp (sym->getName(), "_gp_disp") == 0) { 570 S = (Inst_t)(intptr_t)sym->getAddress(EM_MIPS); 571 } 572 *inst |= ((S + A) & 0xFFFF); 573 break; 574 575 case R_MIPS_GOT16: 576 case R_MIPS_CALL16: 577 { 578 *inst &= 0xFFFF0000; 579 A = A & 0xFFFF; 580 if (rel->getType() == R_MIPS_GOT16) { 581 if (sym->getBindingAttribute() == STB_LOCAL) { 582 A <<= 16; 583 584 // Find the nearest LO16 relocation type after this entry 585 for (size_t j = i + 1; j < reltab->size(); j++) { 586 ELFRelocTy *this_rel = (*reltab)[j]; 587 ELFSymbolTy *this_sym = (*symtab)[this_rel->getSymTabIndex()]; 588 if (this_rel->getType() == R_MIPS_LO16 && this_sym == sym) { 589 Inst_t *this_inst = (Inst_t *)&(*text)[this_rel->getOffset()]; 590 Inst_t this_A = (Inst_t)(uintptr_t)*this_inst; 591 this_A = this_A & 0xFFFF; 592 A += (short)this_A; 593 break; 594 } 595 } 596 } 597 else { 598 rsl_assert(A == 0 && "R_MIPS_GOT16 addend is not 0."); 599 } 600 } 601 else { // R_MIPS_CALL16 602 rsl_assert(A == 0 && "R_MIPS_CALL16 addend is not 0."); 603 } 604 int got_index = search_got((int)rel->getSymTabIndex(), (void *)(S + A), 605 sym->getBindingAttribute()); 606 int got_offset = (got_index << 2) - GP_OFFSET; 607 *inst |= (got_offset & 0xFFFF); 608 } 609 break; 610 611 case R_MIPS_GPREL32: 612 *inst = A + S - ((int)(intptr_t)got_address() + GP_OFFSET); 613 break; 614 } 615 } 616} 617 618 619// TODO: Refactor all relocations. 620template <unsigned Bitwidth> 621inline void ELFObject<Bitwidth>:: 622relocate(void *(*find_sym)(void *context, char const *name), void *context) { 623 // Init SHNCommonDataSize. 624 // Need refactoring 625 size_t SHNCommonDataSize = 0; 626 627 ELFSectionSymTabTy *symtab = 628 static_cast<ELFSectionSymTabTy *>(getSectionByName(".symtab")); 629 rsl_assert(symtab && "Symtab is required."); 630 631 for (size_t i = 0; i < symtab->size(); ++i) { 632 ELFSymbolTy *sym = (*symtab)[i]; 633 634 if (sym->getType() != STT_OBJECT) { 635 continue; 636 } 637 638 size_t idx = (size_t)sym->getSectionIndex(); 639 switch (idx) { 640 default: 641 if ((*shtab)[idx]->getType() == SHT_NOBITS) { 642 // FIXME(logan): This is a workaround for .lcomm directives 643 // bug of LLVM ARM MC code generator. Remove this when the 644 // LLVM bug is fixed. 645 646 size_t align = 16; 647 SHNCommonDataSize += (size_t)sym->getSize() + align; 648 } 649 break; 650 651 case SHN_COMMON: 652 { 653 size_t align = (size_t)sym->getValue(); 654 SHNCommonDataSize += (size_t)sym->getSize() + align; 655 } 656 break; 657 658 case SHN_ABS: 659 case SHN_UNDEF: 660 case SHN_XINDEX: 661 break; 662 } 663 } 664 if (!initSHNCommonDataSize(SHNCommonDataSize)) { 665 rsl_assert("Allocate memory for common variable fail!"); 666 } 667 668 for (size_t i = 0; i < stab.size(); ++i) { 669 ELFSectionHeaderTy *sh = (*shtab)[i]; 670 if (sh->getType() != SHT_REL && sh->getType() != SHT_RELA) { 671 continue; 672 } 673 ELFSectionRelTableTy *reltab = 674 static_cast<ELFSectionRelTableTy *>(stab[i]); 675 rsl_assert(reltab && "Relocation section can't be NULL."); 676 677 const char *reltab_name = sh->getName(); 678 const char *need_rel_name; 679 if (sh->getType() == SHT_REL) { 680 need_rel_name = reltab_name + 4; 681 // ".rel.xxxx" 682 // ^ start from here. 683 } else { 684 need_rel_name = reltab_name + 5; 685 } 686 687 ELFSectionProgBitsTy *need_rel = 688 static_cast<ELFSectionProgBitsTy *>(getSectionByName(need_rel_name)); 689 rsl_assert(need_rel && "Need be relocated section can't be NULL."); 690 691 switch (getHeader()->getMachine()) { 692 case EM_ARM: 693 relocateARM(find_sym, context, reltab, need_rel); 694 break; 695 case EM_386: 696 relocateX86_32(find_sym, context, reltab, need_rel); 697 break; 698 case EM_X86_64: 699 relocateX86_64(find_sym, context, reltab, need_rel); 700 break; 701 case EM_MIPS: 702 relocateMIPS(find_sym, context, reltab, need_rel); 703 break; 704 705 default: 706 rsl_assert(0 && "Only support ARM, MIPS, X86, and X86_64 relocation."); 707 break; 708 } 709 } 710 711 for (size_t i = 0; i < stab.size(); ++i) { 712 ELFSectionHeaderTy *sh = (*shtab)[i]; 713 if (sh->getType() == SHT_PROGBITS || sh->getType() == SHT_NOBITS) { 714 if (stab[i]) { 715 static_cast<ELFSectionBitsTy *>(stab[i])->protect(); 716 } 717 } 718 } 719} 720 721template <unsigned Bitwidth> 722inline void ELFObject<Bitwidth>::print() const { 723 header->print(); 724 shtab->print(); 725 726 for (size_t i = 0; i < stab.size(); ++i) { 727 ELFSectionTy *sec = stab[i]; 728 if (sec) { 729 sec->print(); 730 } 731 } 732} 733 734#endif // ELF_OBJECT_HXX 735