ARM 64 uses -fpatchable-function-entry=4,2 which adds padding before the function and the addresses in the mcount_loc point there instead of the function entry that is returned by nm. In order to find a function from nm to make sure it's not an unused weak function, the entries in the mcount_loc section needs to match the entries from nm. Since it can be an instruction before the entry, add a before_func variable that ARM 64 can set to 8, and if the mcount_loc entry is within 8 bytes of the nm function entry, then it will be considered a match.
Unified · Split
| @@ -611,13 +611,16 @@ static int add_field(uint64_t addr, uint64_t size) · 610 unmodified lines | |||
| 611 | return 0; | 611 | return 0; |
| 612 | } | 612 | } |
| 613 | 613 | ||
| 614 | /* Used for when mcount/fentry is before the function entry */ | ||
| 615 | static int before_func; | ||
| 616 | |||
| 614 | /* Only return match if the address lies inside the function size */ | 617 | /* Only return match if the address lies inside the function size */ |
| 615 | static int cmp_func_addr(const void *K, const void *A) | 618 | static int cmp_func_addr(const void *K, const void *A) |
| 616 | { | 619 | { |
| 617 | uint64_t key = *(const uint64_t *)K; | 620 | uint64_t key = *(const uint64_t *)K; |
| 618 | const struct func_info *a = A; | 621 | const struct func_info *a = A; |
| 619 | 622 | ||
| 620 | if (key < a->addr) | 623 | if (key + before_func < a->addr) |
| 621 | return -1; | 624 | return -1; |
| 622 | return key >= a->addr + a->size; | 625 | return key >= a->addr + a->size; |
| 623 | } | 626 | } |
| @@ -1253,6 +1256,8 @@ static int do_file(char const *const fname, void *addr) · 629 unmodified lines | |||
| 1253 | #ifdef MCOUNT_SORT_ENABLED | 1256 | #ifdef MCOUNT_SORT_ENABLED |
| 1254 | sort_reloc = true; | 1257 | sort_reloc = true; |
| 1255 | rela_type = 0x403; | 1258 | rela_type = 0x403; |
| 1259 | /* arm64 uses patchable function entry placing before function */ | ||
| 1260 | before_func = 8; | ||
| 1256 | #endif | 1261 | #endif |
| 1257 | /* fallthrough */ | 1262 | /* fallthrough */ |
| 1258 | case EM_386: | 1263 | case EM_386: |