110916706SShile Zhang /* SPDX-License-Identifier: GPL-2.0-only */
210916706SShile Zhang /*
310916706SShile Zhang * sorttable.h
410916706SShile Zhang *
557fa1899SShile Zhang * Added ORC unwind tables sort support and other updates:
657fa1899SShile Zhang * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
757fa1899SShile Zhang * Shile Zhang <shile.zhang@linux.alibaba.com>
857fa1899SShile Zhang *
910916706SShile Zhang * Copyright 2011 - 2012 Cavium, Inc.
1010916706SShile Zhang *
1157fa1899SShile Zhang * Some of code was taken out of arch/x86/kernel/unwind_orc.c, written by:
1257fa1899SShile Zhang * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
1357fa1899SShile Zhang *
1410916706SShile Zhang * Some of this code was taken out of recordmcount.h written by:
1510916706SShile Zhang *
1610916706SShile Zhang * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
1710916706SShile Zhang * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
1810916706SShile Zhang */
1910916706SShile Zhang
2010916706SShile Zhang #undef extable_ent_size
2110916706SShile Zhang #undef compare_extable
2272b3942aSYinan Liu #undef get_mcount_loc
2372b3942aSYinan Liu #undef sort_mcount_loc
2472b3942aSYinan Liu #undef elf_mcount_loc
2510916706SShile Zhang #undef do_sort
2610916706SShile Zhang #undef Elf_Addr
2710916706SShile Zhang #undef Elf_Ehdr
2810916706SShile Zhang #undef Elf_Shdr
2910916706SShile Zhang #undef Elf_Rel
3010916706SShile Zhang #undef Elf_Rela
3110916706SShile Zhang #undef Elf_Sym
3210916706SShile Zhang #undef ELF_R_SYM
3310916706SShile Zhang #undef Elf_r_sym
3410916706SShile Zhang #undef ELF_R_INFO
3510916706SShile Zhang #undef Elf_r_info
3610916706SShile Zhang #undef ELF_ST_BIND
3710916706SShile Zhang #undef ELF_ST_TYPE
3810916706SShile Zhang #undef fn_ELF_R_SYM
3910916706SShile Zhang #undef fn_ELF_R_INFO
4010916706SShile Zhang #undef uint_t
4110916706SShile Zhang #undef _r
4210916706SShile Zhang #undef _w
4310916706SShile Zhang
4410916706SShile Zhang #ifdef SORTTABLE_64
4510916706SShile Zhang # define extable_ent_size 16
4610916706SShile Zhang # define compare_extable compare_extable_64
4772b3942aSYinan Liu # define get_mcount_loc get_mcount_loc_64
4872b3942aSYinan Liu # define sort_mcount_loc sort_mcount_loc_64
4972b3942aSYinan Liu # define elf_mcount_loc elf_mcount_loc_64
5010916706SShile Zhang # define do_sort do_sort_64
5110916706SShile Zhang # define Elf_Addr Elf64_Addr
5210916706SShile Zhang # define Elf_Ehdr Elf64_Ehdr
5310916706SShile Zhang # define Elf_Shdr Elf64_Shdr
5410916706SShile Zhang # define Elf_Rel Elf64_Rel
5510916706SShile Zhang # define Elf_Rela Elf64_Rela
5610916706SShile Zhang # define Elf_Sym Elf64_Sym
5710916706SShile Zhang # define ELF_R_SYM ELF64_R_SYM
5810916706SShile Zhang # define Elf_r_sym Elf64_r_sym
5910916706SShile Zhang # define ELF_R_INFO ELF64_R_INFO
6010916706SShile Zhang # define Elf_r_info Elf64_r_info
6110916706SShile Zhang # define ELF_ST_BIND ELF64_ST_BIND
6210916706SShile Zhang # define ELF_ST_TYPE ELF64_ST_TYPE
6310916706SShile Zhang # define fn_ELF_R_SYM fn_ELF64_R_SYM
6410916706SShile Zhang # define fn_ELF_R_INFO fn_ELF64_R_INFO
6510916706SShile Zhang # define uint_t uint64_t
6610916706SShile Zhang # define _r r8
6710916706SShile Zhang # define _w w8
6810916706SShile Zhang #else
6910916706SShile Zhang # define extable_ent_size 8
7010916706SShile Zhang # define compare_extable compare_extable_32
7172b3942aSYinan Liu # define get_mcount_loc get_mcount_loc_32
7272b3942aSYinan Liu # define sort_mcount_loc sort_mcount_loc_32
7372b3942aSYinan Liu # define elf_mcount_loc elf_mcount_loc_32
7410916706SShile Zhang # define do_sort do_sort_32
7510916706SShile Zhang # define Elf_Addr Elf32_Addr
7610916706SShile Zhang # define Elf_Ehdr Elf32_Ehdr
7710916706SShile Zhang # define Elf_Shdr Elf32_Shdr
7810916706SShile Zhang # define Elf_Rel Elf32_Rel
7910916706SShile Zhang # define Elf_Rela Elf32_Rela
8010916706SShile Zhang # define Elf_Sym Elf32_Sym
8110916706SShile Zhang # define ELF_R_SYM ELF32_R_SYM
8210916706SShile Zhang # define Elf_r_sym Elf32_r_sym
8310916706SShile Zhang # define ELF_R_INFO ELF32_R_INFO
8410916706SShile Zhang # define Elf_r_info Elf32_r_info
8510916706SShile Zhang # define ELF_ST_BIND ELF32_ST_BIND
8610916706SShile Zhang # define ELF_ST_TYPE ELF32_ST_TYPE
8710916706SShile Zhang # define fn_ELF_R_SYM fn_ELF32_R_SYM
8810916706SShile Zhang # define fn_ELF_R_INFO fn_ELF32_R_INFO
8910916706SShile Zhang # define uint_t uint32_t
9010916706SShile Zhang # define _r r
9110916706SShile Zhang # define _w w
9210916706SShile Zhang #endif
9310916706SShile Zhang
9457fa1899SShile Zhang #if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
9557fa1899SShile Zhang /* ORC unwinder only support X86_64 */
9657fa1899SShile Zhang #include <asm/orc_types.h>
9757fa1899SShile Zhang
9857fa1899SShile Zhang #define ERRSTR_MAXSZ 256
9957fa1899SShile Zhang
10057fa1899SShile Zhang char g_err[ERRSTR_MAXSZ];
10157fa1899SShile Zhang int *g_orc_ip_table;
10257fa1899SShile Zhang struct orc_entry *g_orc_table;
10357fa1899SShile Zhang
10457fa1899SShile Zhang pthread_t orc_sort_thread;
10557fa1899SShile Zhang
orc_ip(const int * ip)10657fa1899SShile Zhang static inline unsigned long orc_ip(const int *ip)
10757fa1899SShile Zhang {
10857fa1899SShile Zhang return (unsigned long)ip + *ip;
10957fa1899SShile Zhang }
11057fa1899SShile Zhang
orc_sort_cmp(const void * _a,const void * _b)11157fa1899SShile Zhang static int orc_sort_cmp(const void *_a, const void *_b)
11257fa1899SShile Zhang {
11357fa1899SShile Zhang struct orc_entry *orc_a;
11457fa1899SShile Zhang const int *a = g_orc_ip_table + *(int *)_a;
11557fa1899SShile Zhang const int *b = g_orc_ip_table + *(int *)_b;
11657fa1899SShile Zhang unsigned long a_val = orc_ip(a);
11757fa1899SShile Zhang unsigned long b_val = orc_ip(b);
11857fa1899SShile Zhang
11957fa1899SShile Zhang if (a_val > b_val)
12057fa1899SShile Zhang return 1;
12157fa1899SShile Zhang if (a_val < b_val)
12257fa1899SShile Zhang return -1;
12357fa1899SShile Zhang
12457fa1899SShile Zhang /*
12557fa1899SShile Zhang * The "weak" section terminator entries need to always be on the left
12657fa1899SShile Zhang * to ensure the lookup code skips them in favor of real entries.
12757fa1899SShile Zhang * These terminator entries exist to handle any gaps created by
12857fa1899SShile Zhang * whitelisted .o files which didn't get objtool generation.
12957fa1899SShile Zhang */
13057fa1899SShile Zhang orc_a = g_orc_table + (a - g_orc_ip_table);
131*fb799447SJosh Poimboeuf return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1;
13257fa1899SShile Zhang }
13357fa1899SShile Zhang
sort_orctable(void * arg)13457fa1899SShile Zhang static void *sort_orctable(void *arg)
13557fa1899SShile Zhang {
13657fa1899SShile Zhang int i;
13757fa1899SShile Zhang int *idxs = NULL;
13857fa1899SShile Zhang int *tmp_orc_ip_table = NULL;
13957fa1899SShile Zhang struct orc_entry *tmp_orc_table = NULL;
14057fa1899SShile Zhang unsigned int *orc_ip_size = (unsigned int *)arg;
14157fa1899SShile Zhang unsigned int num_entries = *orc_ip_size / sizeof(int);
14257fa1899SShile Zhang unsigned int orc_size = num_entries * sizeof(struct orc_entry);
14357fa1899SShile Zhang
14457fa1899SShile Zhang idxs = (int *)malloc(*orc_ip_size);
14557fa1899SShile Zhang if (!idxs) {
14657fa1899SShile Zhang snprintf(g_err, ERRSTR_MAXSZ, "malloc idxs: %s",
14757fa1899SShile Zhang strerror(errno));
14857fa1899SShile Zhang pthread_exit(g_err);
14957fa1899SShile Zhang }
15057fa1899SShile Zhang
15157fa1899SShile Zhang tmp_orc_ip_table = (int *)malloc(*orc_ip_size);
15257fa1899SShile Zhang if (!tmp_orc_ip_table) {
15357fa1899SShile Zhang snprintf(g_err, ERRSTR_MAXSZ, "malloc tmp_orc_ip_table: %s",
15457fa1899SShile Zhang strerror(errno));
15557fa1899SShile Zhang pthread_exit(g_err);
15657fa1899SShile Zhang }
15757fa1899SShile Zhang
15857fa1899SShile Zhang tmp_orc_table = (struct orc_entry *)malloc(orc_size);
15957fa1899SShile Zhang if (!tmp_orc_table) {
16057fa1899SShile Zhang snprintf(g_err, ERRSTR_MAXSZ, "malloc tmp_orc_table: %s",
16157fa1899SShile Zhang strerror(errno));
16257fa1899SShile Zhang pthread_exit(g_err);
16357fa1899SShile Zhang }
16457fa1899SShile Zhang
16557fa1899SShile Zhang /* initialize indices array, convert ip_table to absolute address */
16657fa1899SShile Zhang for (i = 0; i < num_entries; i++) {
16757fa1899SShile Zhang idxs[i] = i;
16857fa1899SShile Zhang tmp_orc_ip_table[i] = g_orc_ip_table[i] + i * sizeof(int);
16957fa1899SShile Zhang }
17057fa1899SShile Zhang memcpy(tmp_orc_table, g_orc_table, orc_size);
17157fa1899SShile Zhang
17257fa1899SShile Zhang qsort(idxs, num_entries, sizeof(int), orc_sort_cmp);
17357fa1899SShile Zhang
17457fa1899SShile Zhang for (i = 0; i < num_entries; i++) {
17557fa1899SShile Zhang if (idxs[i] == i)
17657fa1899SShile Zhang continue;
17757fa1899SShile Zhang
17857fa1899SShile Zhang /* convert back to relative address */
17957fa1899SShile Zhang g_orc_ip_table[i] = tmp_orc_ip_table[idxs[i]] - i * sizeof(int);
18057fa1899SShile Zhang g_orc_table[i] = tmp_orc_table[idxs[i]];
18157fa1899SShile Zhang }
18257fa1899SShile Zhang
18357fa1899SShile Zhang free(idxs);
18457fa1899SShile Zhang free(tmp_orc_ip_table);
18557fa1899SShile Zhang free(tmp_orc_table);
18657fa1899SShile Zhang pthread_exit(NULL);
18757fa1899SShile Zhang }
18857fa1899SShile Zhang #endif
18957fa1899SShile Zhang
compare_extable(const void * a,const void * b)19010916706SShile Zhang static int compare_extable(const void *a, const void *b)
19110916706SShile Zhang {
19210916706SShile Zhang Elf_Addr av = _r(a);
19310916706SShile Zhang Elf_Addr bv = _r(b);
19410916706SShile Zhang
19510916706SShile Zhang if (av < bv)
19610916706SShile Zhang return -1;
19710916706SShile Zhang if (av > bv)
19810916706SShile Zhang return 1;
19910916706SShile Zhang return 0;
20010916706SShile Zhang }
20172b3942aSYinan Liu #ifdef MCOUNT_SORT_ENABLED
20235140d39SYinan Liu pthread_t mcount_sort_thread;
20335140d39SYinan Liu
20472b3942aSYinan Liu struct elf_mcount_loc {
20572b3942aSYinan Liu Elf_Ehdr *ehdr;
20672b3942aSYinan Liu Elf_Shdr *init_data_sec;
20772b3942aSYinan Liu uint_t start_mcount_loc;
20872b3942aSYinan Liu uint_t stop_mcount_loc;
20972b3942aSYinan Liu };
21010916706SShile Zhang
21172b3942aSYinan Liu /* Sort the addresses stored between __start_mcount_loc to __stop_mcount_loc in vmlinux */
sort_mcount_loc(void * arg)21272b3942aSYinan Liu static void *sort_mcount_loc(void *arg)
21372b3942aSYinan Liu {
21472b3942aSYinan Liu struct elf_mcount_loc *emloc = (struct elf_mcount_loc *)arg;
21572b3942aSYinan Liu uint_t offset = emloc->start_mcount_loc - _r(&(emloc->init_data_sec)->sh_addr)
21672b3942aSYinan Liu + _r(&(emloc->init_data_sec)->sh_offset);
21772b3942aSYinan Liu uint_t count = emloc->stop_mcount_loc - emloc->start_mcount_loc;
21872b3942aSYinan Liu unsigned char *start_loc = (void *)emloc->ehdr + offset;
21972b3942aSYinan Liu
22072b3942aSYinan Liu qsort(start_loc, count/sizeof(uint_t), sizeof(uint_t), compare_extable);
22172b3942aSYinan Liu return NULL;
22272b3942aSYinan Liu }
22372b3942aSYinan Liu
22472b3942aSYinan Liu /* Get the address of __start_mcount_loc and __stop_mcount_loc in System.map */
get_mcount_loc(uint_t * _start,uint_t * _stop)22572b3942aSYinan Liu static void get_mcount_loc(uint_t *_start, uint_t *_stop)
22672b3942aSYinan Liu {
22772b3942aSYinan Liu FILE *file_start, *file_stop;
22872b3942aSYinan Liu char start_buff[20];
22972b3942aSYinan Liu char stop_buff[20];
23072b3942aSYinan Liu int len = 0;
23172b3942aSYinan Liu
23272b3942aSYinan Liu file_start = popen(" grep start_mcount System.map | awk '{print $1}' ", "r");
23372b3942aSYinan Liu if (!file_start) {
23472b3942aSYinan Liu fprintf(stderr, "get start_mcount_loc error!");
23572b3942aSYinan Liu return;
23672b3942aSYinan Liu }
23772b3942aSYinan Liu
23872b3942aSYinan Liu file_stop = popen(" grep stop_mcount System.map | awk '{print $1}' ", "r");
23972b3942aSYinan Liu if (!file_stop) {
24072b3942aSYinan Liu fprintf(stderr, "get stop_mcount_loc error!");
24172b3942aSYinan Liu pclose(file_start);
24272b3942aSYinan Liu return;
24372b3942aSYinan Liu }
24472b3942aSYinan Liu
24572b3942aSYinan Liu while (fgets(start_buff, sizeof(start_buff), file_start) != NULL) {
24672b3942aSYinan Liu len = strlen(start_buff);
24772b3942aSYinan Liu start_buff[len - 1] = '\0';
24872b3942aSYinan Liu }
24972b3942aSYinan Liu *_start = strtoul(start_buff, NULL, 16);
25072b3942aSYinan Liu
25172b3942aSYinan Liu while (fgets(stop_buff, sizeof(stop_buff), file_stop) != NULL) {
25272b3942aSYinan Liu len = strlen(stop_buff);
25372b3942aSYinan Liu stop_buff[len - 1] = '\0';
25472b3942aSYinan Liu }
25572b3942aSYinan Liu *_stop = strtoul(stop_buff, NULL, 16);
25672b3942aSYinan Liu
25772b3942aSYinan Liu pclose(file_start);
25872b3942aSYinan Liu pclose(file_stop);
25972b3942aSYinan Liu }
26072b3942aSYinan Liu #endif
do_sort(Elf_Ehdr * ehdr,char const * const fname,table_sort_t custom_sort)26110916706SShile Zhang static int do_sort(Elf_Ehdr *ehdr,
26210916706SShile Zhang char const *const fname,
26310916706SShile Zhang table_sort_t custom_sort)
26410916706SShile Zhang {
26557fa1899SShile Zhang int rc = -1;
26610916706SShile Zhang Elf_Shdr *s, *shdr = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->e_shoff));
26710916706SShile Zhang Elf_Shdr *strtab_sec = NULL;
26810916706SShile Zhang Elf_Shdr *symtab_sec = NULL;
26910916706SShile Zhang Elf_Shdr *extab_sec = NULL;
27010916706SShile Zhang Elf_Sym *sym;
27110916706SShile Zhang const Elf_Sym *symtab;
27210916706SShile Zhang Elf32_Word *symtab_shndx = NULL;
27310916706SShile Zhang Elf_Sym *sort_needed_sym = NULL;
27410916706SShile Zhang Elf_Shdr *sort_needed_sec;
27510916706SShile Zhang Elf_Rel *relocs = NULL;
27610916706SShile Zhang int relocs_size = 0;
27710916706SShile Zhang uint32_t *sort_needed_loc;
27810916706SShile Zhang const char *secstrings;
27910916706SShile Zhang const char *strtab;
28010916706SShile Zhang char *extab_image;
28110916706SShile Zhang int extab_index = 0;
28210916706SShile Zhang int i;
28310916706SShile Zhang int idx;
28410916706SShile Zhang unsigned int shnum;
28510916706SShile Zhang unsigned int shstrndx;
28672b3942aSYinan Liu #ifdef MCOUNT_SORT_ENABLED
28735140d39SYinan Liu struct elf_mcount_loc mstruct = {0};
28872b3942aSYinan Liu uint_t _start_mcount_loc = 0;
28972b3942aSYinan Liu uint_t _stop_mcount_loc = 0;
29072b3942aSYinan Liu #endif
29157fa1899SShile Zhang #if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
29257fa1899SShile Zhang unsigned int orc_ip_size = 0;
29357fa1899SShile Zhang unsigned int orc_size = 0;
29457fa1899SShile Zhang unsigned int orc_num_entries = 0;
29557fa1899SShile Zhang #endif
29610916706SShile Zhang
29710916706SShile Zhang shstrndx = r2(&ehdr->e_shstrndx);
29810916706SShile Zhang if (shstrndx == SHN_XINDEX)
29910916706SShile Zhang shstrndx = r(&shdr[0].sh_link);
30010916706SShile Zhang secstrings = (const char *)ehdr + _r(&shdr[shstrndx].sh_offset);
30110916706SShile Zhang
30210916706SShile Zhang shnum = r2(&ehdr->e_shnum);
30310916706SShile Zhang if (shnum == SHN_UNDEF)
30410916706SShile Zhang shnum = _r(&shdr[0].sh_size);
30510916706SShile Zhang
30610916706SShile Zhang for (i = 0, s = shdr; s < shdr + shnum; i++, s++) {
30710916706SShile Zhang idx = r(&s->sh_name);
30810916706SShile Zhang if (!strcmp(secstrings + idx, "__ex_table")) {
30910916706SShile Zhang extab_sec = s;
31010916706SShile Zhang extab_index = i;
31110916706SShile Zhang }
31210916706SShile Zhang if (!strcmp(secstrings + idx, ".symtab"))
31310916706SShile Zhang symtab_sec = s;
31410916706SShile Zhang if (!strcmp(secstrings + idx, ".strtab"))
31510916706SShile Zhang strtab_sec = s;
31610916706SShile Zhang
31710916706SShile Zhang if ((r(&s->sh_type) == SHT_REL ||
31810916706SShile Zhang r(&s->sh_type) == SHT_RELA) &&
31910916706SShile Zhang r(&s->sh_info) == extab_index) {
32010916706SShile Zhang relocs = (void *)ehdr + _r(&s->sh_offset);
32110916706SShile Zhang relocs_size = _r(&s->sh_size);
32210916706SShile Zhang }
32310916706SShile Zhang if (r(&s->sh_type) == SHT_SYMTAB_SHNDX)
32410916706SShile Zhang symtab_shndx = (Elf32_Word *)((const char *)ehdr +
32510916706SShile Zhang _r(&s->sh_offset));
32657fa1899SShile Zhang
32772b3942aSYinan Liu #ifdef MCOUNT_SORT_ENABLED
32872b3942aSYinan Liu /* locate the .init.data section in vmlinux */
32972b3942aSYinan Liu if (!strcmp(secstrings + idx, ".init.data")) {
33072b3942aSYinan Liu get_mcount_loc(&_start_mcount_loc, &_stop_mcount_loc);
33172b3942aSYinan Liu mstruct.ehdr = ehdr;
33272b3942aSYinan Liu mstruct.init_data_sec = s;
33372b3942aSYinan Liu mstruct.start_mcount_loc = _start_mcount_loc;
33472b3942aSYinan Liu mstruct.stop_mcount_loc = _stop_mcount_loc;
33572b3942aSYinan Liu }
33672b3942aSYinan Liu #endif
33772b3942aSYinan Liu
33857fa1899SShile Zhang #if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
33957fa1899SShile Zhang /* locate the ORC unwind tables */
34057fa1899SShile Zhang if (!strcmp(secstrings + idx, ".orc_unwind_ip")) {
34157fa1899SShile Zhang orc_ip_size = s->sh_size;
34257fa1899SShile Zhang g_orc_ip_table = (int *)((void *)ehdr +
34357fa1899SShile Zhang s->sh_offset);
34457fa1899SShile Zhang }
34557fa1899SShile Zhang if (!strcmp(secstrings + idx, ".orc_unwind")) {
34657fa1899SShile Zhang orc_size = s->sh_size;
34757fa1899SShile Zhang g_orc_table = (struct orc_entry *)((void *)ehdr +
34857fa1899SShile Zhang s->sh_offset);
34957fa1899SShile Zhang }
35057fa1899SShile Zhang #endif
35157fa1899SShile Zhang } /* for loop */
35257fa1899SShile Zhang
35357fa1899SShile Zhang #if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
35457fa1899SShile Zhang if (!g_orc_ip_table || !g_orc_table) {
35557fa1899SShile Zhang fprintf(stderr,
35657fa1899SShile Zhang "incomplete ORC unwind tables in file: %s\n", fname);
35757fa1899SShile Zhang goto out;
35810916706SShile Zhang }
35910916706SShile Zhang
36057fa1899SShile Zhang orc_num_entries = orc_ip_size / sizeof(int);
36157fa1899SShile Zhang if (orc_ip_size % sizeof(int) != 0 ||
36257fa1899SShile Zhang orc_size % sizeof(struct orc_entry) != 0 ||
36357fa1899SShile Zhang orc_num_entries != orc_size / sizeof(struct orc_entry)) {
36457fa1899SShile Zhang fprintf(stderr,
36557fa1899SShile Zhang "inconsistent ORC unwind table entries in file: %s\n",
36657fa1899SShile Zhang fname);
36757fa1899SShile Zhang goto out;
36857fa1899SShile Zhang }
36957fa1899SShile Zhang
37057fa1899SShile Zhang /* create thread to sort ORC unwind tables concurrently */
37157fa1899SShile Zhang if (pthread_create(&orc_sort_thread, NULL,
37257fa1899SShile Zhang sort_orctable, &orc_ip_size)) {
37357fa1899SShile Zhang fprintf(stderr,
37457fa1899SShile Zhang "pthread_create orc_sort_thread failed '%s': %s\n",
37557fa1899SShile Zhang strerror(errno), fname);
37657fa1899SShile Zhang goto out;
37757fa1899SShile Zhang }
37857fa1899SShile Zhang #endif
37972b3942aSYinan Liu
38072b3942aSYinan Liu #ifdef MCOUNT_SORT_ENABLED
38172b3942aSYinan Liu if (!mstruct.init_data_sec || !_start_mcount_loc || !_stop_mcount_loc) {
38272b3942aSYinan Liu fprintf(stderr,
38372b3942aSYinan Liu "incomplete mcount's sort in file: %s\n",
38472b3942aSYinan Liu fname);
38572b3942aSYinan Liu goto out;
38672b3942aSYinan Liu }
38772b3942aSYinan Liu
38872b3942aSYinan Liu /* create thread to sort mcount_loc concurrently */
38972b3942aSYinan Liu if (pthread_create(&mcount_sort_thread, NULL, &sort_mcount_loc, &mstruct)) {
39072b3942aSYinan Liu fprintf(stderr,
39172b3942aSYinan Liu "pthread_create mcount_sort_thread failed '%s': %s\n",
39272b3942aSYinan Liu strerror(errno), fname);
39372b3942aSYinan Liu goto out;
39472b3942aSYinan Liu }
39572b3942aSYinan Liu #endif
39610916706SShile Zhang if (!extab_sec) {
39710916706SShile Zhang fprintf(stderr, "no __ex_table in file: %s\n", fname);
39857fa1899SShile Zhang goto out;
39910916706SShile Zhang }
40010916706SShile Zhang
40110916706SShile Zhang if (!symtab_sec) {
40210916706SShile Zhang fprintf(stderr, "no .symtab in file: %s\n", fname);
40357fa1899SShile Zhang goto out;
40410916706SShile Zhang }
40510916706SShile Zhang
40610916706SShile Zhang if (!strtab_sec) {
40710916706SShile Zhang fprintf(stderr, "no .strtab in file: %s\n", fname);
40857fa1899SShile Zhang goto out;
40910916706SShile Zhang }
41010916706SShile Zhang
41110916706SShile Zhang extab_image = (void *)ehdr + _r(&extab_sec->sh_offset);
41210916706SShile Zhang strtab = (const char *)ehdr + _r(&strtab_sec->sh_offset);
41310916706SShile Zhang symtab = (const Elf_Sym *)((const char *)ehdr +
41410916706SShile Zhang _r(&symtab_sec->sh_offset));
41510916706SShile Zhang
41610916706SShile Zhang if (custom_sort) {
41710916706SShile Zhang custom_sort(extab_image, _r(&extab_sec->sh_size));
41810916706SShile Zhang } else {
41910916706SShile Zhang int num_entries = _r(&extab_sec->sh_size) / extable_ent_size;
42010916706SShile Zhang qsort(extab_image, num_entries,
42110916706SShile Zhang extable_ent_size, compare_extable);
42210916706SShile Zhang }
42310916706SShile Zhang
42410916706SShile Zhang /* If there were relocations, we no longer need them. */
42510916706SShile Zhang if (relocs)
42610916706SShile Zhang memset(relocs, 0, relocs_size);
42710916706SShile Zhang
42810916706SShile Zhang /* find the flag main_extable_sort_needed */
42910916706SShile Zhang for (sym = (void *)ehdr + _r(&symtab_sec->sh_offset);
43010916706SShile Zhang sym < sym + _r(&symtab_sec->sh_size) / sizeof(Elf_Sym);
43110916706SShile Zhang sym++) {
43210916706SShile Zhang if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
43310916706SShile Zhang continue;
43410916706SShile Zhang if (!strcmp(strtab + r(&sym->st_name),
43510916706SShile Zhang "main_extable_sort_needed")) {
43610916706SShile Zhang sort_needed_sym = sym;
43710916706SShile Zhang break;
43810916706SShile Zhang }
43910916706SShile Zhang }
44010916706SShile Zhang
44110916706SShile Zhang if (!sort_needed_sym) {
44210916706SShile Zhang fprintf(stderr,
44310916706SShile Zhang "no main_extable_sort_needed symbol in file: %s\n",
44410916706SShile Zhang fname);
44557fa1899SShile Zhang goto out;
44610916706SShile Zhang }
44710916706SShile Zhang
44810916706SShile Zhang sort_needed_sec = &shdr[get_secindex(r2(&sym->st_shndx),
44910916706SShile Zhang sort_needed_sym - symtab,
45010916706SShile Zhang symtab_shndx)];
45110916706SShile Zhang sort_needed_loc = (void *)ehdr +
45210916706SShile Zhang _r(&sort_needed_sec->sh_offset) +
45310916706SShile Zhang _r(&sort_needed_sym->st_value) -
45410916706SShile Zhang _r(&sort_needed_sec->sh_addr);
45510916706SShile Zhang
45610916706SShile Zhang /* extable has been sorted, clear the flag */
45710916706SShile Zhang w(0, sort_needed_loc);
45857fa1899SShile Zhang rc = 0;
45910916706SShile Zhang
46057fa1899SShile Zhang out:
46157fa1899SShile Zhang #if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
46257fa1899SShile Zhang if (orc_sort_thread) {
46357fa1899SShile Zhang void *retval = NULL;
46457fa1899SShile Zhang /* wait for ORC tables sort done */
46557fa1899SShile Zhang rc = pthread_join(orc_sort_thread, &retval);
466c8a7ff13SYinan Liu if (rc) {
46757fa1899SShile Zhang fprintf(stderr,
46857fa1899SShile Zhang "pthread_join failed '%s': %s\n",
46957fa1899SShile Zhang strerror(errno), fname);
470c8a7ff13SYinan Liu } else if (retval) {
47157fa1899SShile Zhang rc = -1;
47257fa1899SShile Zhang fprintf(stderr,
47357fa1899SShile Zhang "failed to sort ORC tables '%s': %s\n",
47457fa1899SShile Zhang (char *)retval, fname);
47557fa1899SShile Zhang }
47657fa1899SShile Zhang }
47757fa1899SShile Zhang #endif
47872b3942aSYinan Liu
47972b3942aSYinan Liu #ifdef MCOUNT_SORT_ENABLED
48072b3942aSYinan Liu if (mcount_sort_thread) {
48172b3942aSYinan Liu void *retval = NULL;
48272b3942aSYinan Liu /* wait for mcount sort done */
48372b3942aSYinan Liu rc = pthread_join(mcount_sort_thread, &retval);
48472b3942aSYinan Liu if (rc) {
48572b3942aSYinan Liu fprintf(stderr,
48672b3942aSYinan Liu "pthread_join failed '%s': %s\n",
48772b3942aSYinan Liu strerror(errno), fname);
48872b3942aSYinan Liu } else if (retval) {
48972b3942aSYinan Liu rc = -1;
49072b3942aSYinan Liu fprintf(stderr,
49172b3942aSYinan Liu "failed to sort mcount '%s': %s\n",
49272b3942aSYinan Liu (char *)retval, fname);
49372b3942aSYinan Liu }
49472b3942aSYinan Liu }
49572b3942aSYinan Liu #endif
49657fa1899SShile Zhang return rc;
49710916706SShile Zhang }
498