|
1 | 1 | /* |
2 | | - * Copyright (C) 2020-2021 Intel Corporation |
| 2 | + * Copyright (C) 2020-2022 Intel Corporation |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: MIT |
5 | 5 | * |
@@ -347,67 +347,177 @@ struct ElfSymbolEntryTypes<EI_CLASS_64> { |
347 | 347 | }; |
348 | 348 |
|
349 | 349 | template <ELF_IDENTIFIER_CLASS NumBits> |
350 | | -struct ElfSymbolEntry; |
| 350 | +struct ElfSymbolEntry { |
| 351 | + using Name = typename ElfSymbolEntryTypes<NumBits>::Name; |
| 352 | + using Value = typename ElfSymbolEntryTypes<NumBits>::Value; |
| 353 | + using Size = typename ElfSymbolEntryTypes<NumBits>::Size; |
| 354 | + using Info = typename ElfSymbolEntryTypes<NumBits>::Info; |
| 355 | + using Other = typename ElfSymbolEntryTypes<NumBits>::Other; |
| 356 | + using Shndx = typename ElfSymbolEntryTypes<NumBits>::Shndx; |
| 357 | + Name name = 0U; |
| 358 | + Info info = 0U; |
| 359 | + Other other = 0U; |
| 360 | + Shndx shndx = SHN_UNDEF; |
| 361 | + Value value = 0U; |
| 362 | + Size size = 0U; |
| 363 | + |
| 364 | + Info getBinding() const { |
| 365 | + return info >> 4; |
| 366 | + } |
351 | 367 |
|
352 | | -template <> |
353 | | -struct ElfSymbolEntry<EI_CLASS_32> { |
354 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Name name; |
355 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Value value; |
356 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Size size; |
357 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Info info; |
358 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Other other; |
359 | | - ElfSymbolEntryTypes<EI_CLASS_32>::Shndx shndx; |
360 | | -}; |
| 368 | + Info getType() const { |
| 369 | + return info & 0xF; |
| 370 | + } |
361 | 371 |
|
362 | | -template <> |
363 | | -struct ElfSymbolEntry<EI_CLASS_64> { |
364 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Name name; |
365 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Info info; |
366 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Other other; |
367 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Shndx shndx; |
368 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Value value; |
369 | | - ElfSymbolEntryTypes<EI_CLASS_64>::Size size; |
| 372 | + Other getVisibility() const { |
| 373 | + return other & 0x3; |
| 374 | + } |
| 375 | + |
| 376 | + void setBinding(Info binding) { |
| 377 | + info = (info & 0xF) | (binding << 4); |
| 378 | + } |
| 379 | + |
| 380 | + void setType(Info type) { |
| 381 | + info = (info & (~0xF)) | (type & 0xF); |
| 382 | + } |
| 383 | + |
| 384 | + void setVisibility(Other visibility) { |
| 385 | + other = (other & (~0x3)) | (visibility & 0x3); |
| 386 | + } |
370 | 387 | }; |
371 | 388 |
|
372 | 389 | static_assert(sizeof(ElfSymbolEntry<EI_CLASS_32>) == 0x10, ""); |
373 | 390 | static_assert(sizeof(ElfSymbolEntry<EI_CLASS_64>) == 0x18, ""); |
374 | 391 |
|
375 | | -template <ELF_IDENTIFIER_CLASS NumBits> |
376 | | -struct ElfRel; |
| 392 | +template <int NumBits> |
| 393 | +struct ElfRelocationEntryTypes; |
377 | 394 |
|
378 | 395 | template <> |
379 | | -struct ElfRel<EI_CLASS_32> { |
380 | | - uint32_t offset; |
381 | | - uint32_t info; |
| 396 | +struct ElfRelocationEntryTypes<EI_CLASS_32> { |
| 397 | + using Offset = uint32_t; |
| 398 | + using Info = uint32_t; |
| 399 | + using Addend = int32_t; |
382 | 400 | }; |
383 | 401 |
|
384 | 402 | template <> |
385 | | -struct ElfRel<EI_CLASS_64> { |
386 | | - uint64_t offset; |
387 | | - uint64_t info; |
| 403 | +struct ElfRelocationEntryTypes<EI_CLASS_64> { |
| 404 | + using Offset = uint64_t; |
| 405 | + using Info = uint64_t; |
| 406 | + using Addend = int64_t; |
388 | 407 | }; |
389 | 408 |
|
390 | | -static_assert(sizeof(ElfRel<EI_CLASS_32>) == 0x8, ""); |
391 | | -static_assert(sizeof(ElfRel<EI_CLASS_64>) == 0x10, ""); |
| 409 | +namespace RelocationFuncs { |
| 410 | +template <typename T> |
| 411 | +constexpr T getSymbolTableIndex(T info); |
392 | 412 |
|
393 | | -template <ELF_IDENTIFIER_CLASS NumBits> |
394 | | -struct ElfRela; |
| 413 | +template <> |
| 414 | +constexpr ElfRelocationEntryTypes<EI_CLASS_32>::Info getSymbolTableIndex(ElfRelocationEntryTypes<EI_CLASS_32>::Info info) { |
| 415 | + return info >> 8; |
| 416 | +} |
395 | 417 |
|
396 | 418 | template <> |
397 | | -struct ElfRela<EI_CLASS_32> { |
398 | | - uint32_t offset; |
399 | | - uint32_t info; |
400 | | - int32_t addend; |
401 | | -}; |
| 419 | +constexpr ElfRelocationEntryTypes<EI_CLASS_64>::Info getSymbolTableIndex(ElfRelocationEntryTypes<EI_CLASS_64>::Info info) { |
| 420 | + return info >> 32; |
| 421 | +} |
| 422 | + |
| 423 | +template <typename T> |
| 424 | +constexpr T getRelocationType(T info); |
| 425 | + |
| 426 | +template <> |
| 427 | +constexpr ElfRelocationEntryTypes<EI_CLASS_32>::Info getRelocationType(ElfRelocationEntryTypes<EI_CLASS_32>::Info info) { |
| 428 | + return static_cast<uint8_t>(info); |
| 429 | +} |
402 | 430 |
|
403 | 431 | template <> |
404 | | -struct ElfRela<EI_CLASS_64> { |
405 | | - uint64_t offset; |
406 | | - uint64_t info; |
407 | | - int64_t addend; |
| 432 | +constexpr ElfRelocationEntryTypes<EI_CLASS_64>::Info getRelocationType(ElfRelocationEntryTypes<EI_CLASS_64>::Info info) { |
| 433 | + return static_cast<uint32_t>(info); |
| 434 | +} |
| 435 | + |
| 436 | +template <typename T> |
| 437 | +constexpr T setSymbolTableIndex(T info, T index); |
| 438 | + |
| 439 | +template <> |
| 440 | +constexpr ElfRelocationEntryTypes<EI_CLASS_32>::Info setSymbolTableIndex(ElfRelocationEntryTypes<EI_CLASS_32>::Info info, |
| 441 | + ElfRelocationEntryTypes<EI_CLASS_32>::Info index) { |
| 442 | + return (info & 0x000000FF) | (index << 8); |
| 443 | +} |
| 444 | + |
| 445 | +template <> |
| 446 | +constexpr ElfRelocationEntryTypes<EI_CLASS_64>::Info setSymbolTableIndex(ElfRelocationEntryTypes<EI_CLASS_64>::Info info, |
| 447 | + ElfRelocationEntryTypes<EI_CLASS_64>::Info index) { |
| 448 | + return (info & 0x00000000FFFFFFFF) | (index << 32); |
| 449 | +} |
| 450 | + |
| 451 | +template <typename T> |
| 452 | +constexpr T setRelocationType(T info, T type); |
| 453 | + |
| 454 | +template <> |
| 455 | +constexpr ElfRelocationEntryTypes<EI_CLASS_32>::Info setRelocationType(ElfRelocationEntryTypes<EI_CLASS_32>::Info info, |
| 456 | + ElfRelocationEntryTypes<EI_CLASS_32>::Info type) { |
| 457 | + return (info & 0xFFFFFF00) | static_cast<uint8_t>(type); |
| 458 | +} |
| 459 | + |
| 460 | +template <> |
| 461 | +constexpr ElfRelocationEntryTypes<EI_CLASS_64>::Info setRelocationType(ElfRelocationEntryTypes<EI_CLASS_64>::Info info, |
| 462 | + ElfRelocationEntryTypes<EI_CLASS_64>::Info type) { |
| 463 | + return (info & 0xFFFFFFFF00000000) | static_cast<uint32_t>(type); |
| 464 | +} |
| 465 | +} // namespace RelocationFuncs |
| 466 | + |
| 467 | +template <ELF_IDENTIFIER_CLASS NumBits> |
| 468 | +struct ElfRel { |
| 469 | + using Offset = typename ElfRelocationEntryTypes<NumBits>::Offset; |
| 470 | + using Info = typename ElfRelocationEntryTypes<NumBits>::Info; |
| 471 | + Offset offset = 0U; |
| 472 | + Info info = 0U; |
| 473 | + |
| 474 | + constexpr Info getSymbolTableIndex() const { |
| 475 | + return RelocationFuncs::getSymbolTableIndex(info); |
| 476 | + } |
| 477 | + |
| 478 | + constexpr Info getRelocationType() const { |
| 479 | + return RelocationFuncs::getRelocationType(info); |
| 480 | + } |
| 481 | + |
| 482 | + constexpr void setSymbolTableIndex(Info index) { |
| 483 | + info = RelocationFuncs::setSymbolTableIndex(info, index); |
| 484 | + } |
| 485 | + |
| 486 | + constexpr void setRelocationType(Info type) { |
| 487 | + info = RelocationFuncs::setRelocationType(info, type); |
| 488 | + } |
| 489 | +}; |
| 490 | + |
| 491 | +static_assert(sizeof(ElfRel<EI_CLASS_32>) == 0x8, ""); |
| 492 | +static_assert(sizeof(ElfRel<EI_CLASS_64>) == 0x10, ""); |
| 493 | + |
| 494 | +template <int NumBits> |
| 495 | +struct ElfRela { |
| 496 | + using Offset = typename ElfRelocationEntryTypes<NumBits>::Offset; |
| 497 | + using Info = typename ElfRelocationEntryTypes<NumBits>::Info; |
| 498 | + using Addend = typename ElfRelocationEntryTypes<NumBits>::Addend; |
| 499 | + Offset offset = 0U; |
| 500 | + Info info = 0U; |
| 501 | + Addend addend = 0U; |
| 502 | + |
| 503 | + constexpr Info getSymbolTableIndex() const { |
| 504 | + return RelocationFuncs::getSymbolTableIndex(info); |
| 505 | + } |
| 506 | + |
| 507 | + constexpr Info getRelocationType() const { |
| 508 | + return RelocationFuncs::getRelocationType(info); |
| 509 | + } |
| 510 | + |
| 511 | + constexpr void setSymbolTableIndex(Info index) { |
| 512 | + info = RelocationFuncs::setSymbolTableIndex(info, index); |
| 513 | + } |
| 514 | + |
| 515 | + constexpr void setRelocationType(Info type) { |
| 516 | + info = RelocationFuncs::setRelocationType(info, type); |
| 517 | + } |
408 | 518 | }; |
409 | 519 |
|
410 | | -static_assert(sizeof(ElfRela<EI_CLASS_32>) == 0xc, ""); |
| 520 | +static_assert(sizeof(ElfRela<EI_CLASS_32>) == 0xC, ""); |
411 | 521 | static_assert(sizeof(ElfRela<EI_CLASS_64>) == 0x18, ""); |
412 | 522 |
|
413 | 523 | namespace SpecialSectionNames { |
|
0 commit comments