• GNU 链接器ld lang_process() (三)


    一、 bfd_section_already_linked_table_init ()

            在 Binutils 中,"bfd_section_already_linked_table_init()" 用于初始化一个哈希表,其主要目的可能是跟踪已经被链接的节(sections)以避免重复链接。链接过程中,特定的节可能已经被链接到输出文件中,如果不加以跟踪,可能会导致不必要的重复链接。

            这个哈希表的使用可能在链接过程中的某个阶段,例如节合并(section merging)或符号解析(symbol resolution)时,用于检查某个节是否已经被链接,以便做出适当的决策。

    1. if (!bfd_section_already_linked_table_init ()) // 初始化了一个hash table
    2. einfo (_("%P%F: Failed to create hash table\n"));
    1. bfd_boolean
    2. bfd_section_already_linked_table_init (void)
    3. {
    4. return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
    5. already_linked_newfunc,
    6. sizeof (struct bfd_section_already_linked_hash_entry),
    7. 42);
    8. }
    static struct bfd_hash_table _bfd_section_already_linked_table;
    1. struct bfd_section_already_linked_hash_entry
    2. {
    3. struct bfd_hash_entry root;
    4. struct bfd_section_already_linked *entry;
    5. };
    6. struct bfd_section_already_linked
    7. {
    8. struct bfd_section_already_linked *next;
    9. asection *sec;
    10. };
    11. struct bfd_hash_entry
    12. {
    13. /* Next entry for this hash code. */
    14. struct bfd_hash_entry *next;
    15. /* String being hashed. */
    16. const char *string;
    17. /* Hash code. This is the full hash code, not the index into the
    18. table. */
    19. unsigned long hash;
    20. };

    1. static struct bfd_hash_entry *
    2. already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
    3. struct bfd_hash_table *table,
    4. const char *string ATTRIBUTE_UNUSED)
    5. {
    6. struct bfd_section_already_linked_hash_entry *ret =
    7. (struct bfd_section_already_linked_hash_entry *)
    8. bfd_hash_allocate (table, sizeof *ret);
    9. if (ret == NULL)
    10. return NULL;
    11. ret->entry = NULL;
    12. return &ret->root;
    13. }

    1. static struct bfd_hash_entry *
    2. already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
    3. struct bfd_hash_table *table,
    4. const char *string ATTRIBUTE_UNUSED)
    5. {
    6. struct bfd_section_already_linked_hash_entry *ret =
    7. (struct bfd_section_already_linked_hash_entry *)
    8. bfd_hash_allocate (table, sizeof *ret);
    9. if (ret == NULL)
    10. return NULL;
    11. ret->entry = NULL;
    12. return &ret->root;
    13. }

    这里的函数调用中,&_bfd_section_already_linked_table是一个指向哈希表的指针,表示将要初始化的哈希表的地址,用于存储已经链接过的段的信息。这个数据结构解释如下: 

    标有SEC_LINK_ONCE标志的节应仅链接到输出中一次。此例程检查每个节,如果已经链接了相同名称的节,则安排丢弃该节。此代码假定所有相关部分都设置了SEC_LINK_ONCE标志;

    already_linked_newfunc是一个回调函数,用于在哈希表中创建新的条目。

    1. bfd_boolean
    2. bfd_hash_table_init_n (struct bfd_hash_table *table,
    3. struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
    4. struct bfd_hash_table *,
    5. const char *),
    6. unsigned int entsize,
    7. unsigned int size)
    8. {
    9. unsigned long alloc;
    10. alloc = size;
    11. alloc *= sizeof (struct bfd_hash_entry *);
    12. if (alloc / sizeof (struct bfd_hash_entry *) != size)
    13. {
    14. bfd_set_error (bfd_error_no_memory);
    15. return FALSE;
    16. }
    17. table->memory = (void *) objalloc_create ();
    18. if (table->memory == NULL)
    19. {
    20. bfd_set_error (bfd_error_no_memory);
    21. return FALSE;
    22. }
    23. table->table = (struct bfd_hash_entry **)
    24. objalloc_alloc ((struct objalloc *) table->memory, alloc);
    25. if (table->table == NULL)
    26. {
    27. bfd_hash_table_free (table);
    28. bfd_set_error (bfd_error_no_memory);
    29. return FALSE;
    30. }
    31. memset ((void *) table->table, 0, alloc);
    32. table->size = size;
    33. table->entsize = entsize;
    34. table->count = 0;
    35. table->frozen = 0;
    36. table->newfunc = newfunc;
    37. return TRUE;
    38. }

  • 相关阅读:
    力扣 -- 377. 组合总和 Ⅳ
    【牛客编程题】python入门103题(输入&类型,字符串&列表&字典&元组,运算&条件&循环,函数&类&正则)
    淘宝/天猫优惠券查询 API 返回值说明
    java核心编程——IO流
    JS事件绑定
    【基于React-Native做位置信息获取,并展示出来】
    防反接方案,NMOS & PMOS
    appium自动化测试技术
    react 高效高质量搭建后台系统 系列 —— 前端权限
    python的继承知识点总结
  • 原文地址:https://blog.csdn.net/leimeili/article/details/134230913