1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifdef HAVE_DL_ITERATE_PHDR
25bf215546Sopenharmony_ci#include <dlfcn.h>
26bf215546Sopenharmony_ci#include <link.h>
27bf215546Sopenharmony_ci#include <stddef.h>
28bf215546Sopenharmony_ci#include <string.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "build_id.h"
31bf215546Sopenharmony_ci#include "macros.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#ifndef NT_GNU_BUILD_ID
34bf215546Sopenharmony_ci#define NT_GNU_BUILD_ID 3
35bf215546Sopenharmony_ci#endif
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#ifndef ElfW
38bf215546Sopenharmony_ci#define ElfW(type) Elf_##type
39bf215546Sopenharmony_ci#endif
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistruct build_id_note {
42bf215546Sopenharmony_ci   ElfW(Nhdr) nhdr;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   char name[4]; /* Note name for build-id is "GNU\0" */
45bf215546Sopenharmony_ci   uint8_t build_id[0];
46bf215546Sopenharmony_ci};
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistruct callback_data {
49bf215546Sopenharmony_ci   /* Base address of shared object, taken from Dl_info::dli_fbase */
50bf215546Sopenharmony_ci   const void *dli_fbase;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   struct build_id_note *note;
53bf215546Sopenharmony_ci};
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistatic int
56bf215546Sopenharmony_cibuild_id_find_nhdr_callback(struct dl_phdr_info *info, size_t size, void *data_)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci   struct callback_data *data = data_;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   /* Calculate address where shared object is mapped into the process space.
61bf215546Sopenharmony_ci    * (Using the base address and the virtual address of the first LOAD segment)
62bf215546Sopenharmony_ci    */
63bf215546Sopenharmony_ci   void *map_start = NULL;
64bf215546Sopenharmony_ci   for (unsigned i = 0; i < info->dlpi_phnum; i++) {
65bf215546Sopenharmony_ci      if (info->dlpi_phdr[i].p_type == PT_LOAD) {
66bf215546Sopenharmony_ci         map_start = (void *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
67bf215546Sopenharmony_ci         break;
68bf215546Sopenharmony_ci      }
69bf215546Sopenharmony_ci   }
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   if (map_start != data->dli_fbase)
72bf215546Sopenharmony_ci      return 0;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   for (unsigned i = 0; i < info->dlpi_phnum; i++) {
75bf215546Sopenharmony_ci      if (info->dlpi_phdr[i].p_type != PT_NOTE)
76bf215546Sopenharmony_ci         continue;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci      struct build_id_note *note = (void *)(info->dlpi_addr +
79bf215546Sopenharmony_ci                                            info->dlpi_phdr[i].p_vaddr);
80bf215546Sopenharmony_ci      ptrdiff_t len = info->dlpi_phdr[i].p_filesz;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci      while (len >= sizeof(struct build_id_note)) {
83bf215546Sopenharmony_ci         if (note->nhdr.n_type == NT_GNU_BUILD_ID &&
84bf215546Sopenharmony_ci            note->nhdr.n_descsz != 0 &&
85bf215546Sopenharmony_ci            note->nhdr.n_namesz == 4 &&
86bf215546Sopenharmony_ci            memcmp(note->name, "GNU", 4) == 0) {
87bf215546Sopenharmony_ci            data->note = note;
88bf215546Sopenharmony_ci            return 1;
89bf215546Sopenharmony_ci         }
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci         size_t offset = sizeof(ElfW(Nhdr)) +
92bf215546Sopenharmony_ci                         ALIGN_POT(note->nhdr.n_namesz, 4) +
93bf215546Sopenharmony_ci                         ALIGN_POT(note->nhdr.n_descsz, 4);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci         // 05 00 00 00 04 00 00 00 4f 48 4f 53 00 01 00 00 00 00 00 00
96bf215546Sopenharmony_ci         if ((note->nhdr.n_type == 0x534f484f) && (len > 20)) {
97bf215546Sopenharmony_ci            // .note.ohos.ident is not a valid PT_NOTE section, use offset in section header later
98bf215546Sopenharmony_ci            offset = 20;
99bf215546Sopenharmony_ci         }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci         note = (struct build_id_note *)((char *)note + offset);
102bf215546Sopenharmony_ci         len -= offset;
103bf215546Sopenharmony_ci      }
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   return 0;
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ciconst struct build_id_note *
110bf215546Sopenharmony_cibuild_id_find_nhdr_for_addr(const void *addr)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   Dl_info info;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   if (!dladdr(addr, &info))
115bf215546Sopenharmony_ci      return NULL;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   if (!info.dli_fbase)
118bf215546Sopenharmony_ci      return NULL;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   struct callback_data data = {
121bf215546Sopenharmony_ci      .dli_fbase = info.dli_fbase,
122bf215546Sopenharmony_ci      .note = NULL,
123bf215546Sopenharmony_ci   };
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   if (!dl_iterate_phdr(build_id_find_nhdr_callback, &data))
126bf215546Sopenharmony_ci      return NULL;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   return data.note;
129bf215546Sopenharmony_ci}
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ciunsigned
132bf215546Sopenharmony_cibuild_id_length(const struct build_id_note *note)
133bf215546Sopenharmony_ci{
134bf215546Sopenharmony_ci   return note->nhdr.n_descsz;
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ciconst uint8_t *
138bf215546Sopenharmony_cibuild_id_data(const struct build_id_note *note)
139bf215546Sopenharmony_ci{
140bf215546Sopenharmony_ci   return note->build_id;
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci#endif
144