📄 elf.c
字号:
lockstate = rlock_acquire(rtld_bind_lock);
if (handle == NULL || handle == RTLD_SELF) {
void *retaddr;
retaddr = __builtin_return_address(0); /* __GNUC__ only */
if ((obj = obj_from_addr(retaddr)) == NULL)
_rtld_error("Cannot determine caller's shared object");
} else
obj = dlcheck(handle);
if (obj == NULL) {
rlock_release(rtld_bind_lock, lockstate);
return (-1);
}
error = 0;
switch (request) {
case RTLD_DI_LINKMAP:
*((struct link_map const **)p) = &obj->linkmap;
break;
case RTLD_DI_ORIGIN:
error = rtld_dirname(obj->path, p);
break;
case RTLD_DI_SERINFOSIZE:
case RTLD_DI_SERINFO:
error = do_search_info(obj, request, (struct dl_serinfo *)p);
break;
default:
_rtld_error("Invalid request %d passed to dlinfo()", request);
error = -1;
}
rlock_release(rtld_bind_lock, lockstate);
return (error);
}
struct fill_search_info_args {
int request;
unsigned int flags;
Dl_serinfo *serinfo;
Dl_serpath *serpath;
char *strspace;
};
static void *
fill_search_info(const char *dir, size_t dirlen, void *param)
{
struct fill_search_info_args *arg;
arg = param;
if (arg->request == RTLD_DI_SERINFOSIZE) {
arg->serinfo->dls_cnt ++;
arg->serinfo->dls_size += dirlen + 1;
} else {
struct dl_serpath *s_entry;
s_entry = arg->serpath;
s_entry->dls_name = arg->strspace;
s_entry->dls_flags = arg->flags;
strncpy(arg->strspace, dir, dirlen);
arg->strspace[dirlen] = '\0';
arg->strspace += dirlen + 1;
arg->serpath++;
}
return (NULL);
}
static int
do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
{
struct dl_serinfo _info;
struct fill_search_info_args args;
args.request = RTLD_DI_SERINFOSIZE;
args.serinfo = &_info;
_info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
_info.dls_cnt = 0;
path_enumerate(ld_library_path, fill_search_info, &args);
path_enumerate(obj->rpath, fill_search_info, &args);
path_enumerate(gethints(), fill_search_info, &args);
path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args);
if (request == RTLD_DI_SERINFOSIZE) {
info->dls_size = _info.dls_size;
info->dls_cnt = _info.dls_cnt;
return (0);
}
if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
_rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
return (-1);
}
args.request = RTLD_DI_SERINFO;
args.serinfo = info;
args.serpath = &info->dls_serpath[0];
args.strspace = (char *)&info->dls_serpath[_info.dls_cnt];
args.flags = LA_SER_LIBPATH;
if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
return (-1);
args.flags = LA_SER_RUNPATH;
if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
return (-1);
args.flags = LA_SER_CONFIG;
if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
return (-1);
args.flags = LA_SER_DEFAULT;
if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
return (-1);
return (0);
}
static int
rtld_dirname(const char *path, char *bname)
{
const char *endp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
bname[0] = '.';
bname[1] = '\0';
return (0);
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* Find the start of the dir */
while (endp > path && *endp != '/')
endp--;
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
bname[0] = *endp == '/' ? '/' : '.';
bname[1] = '\0';
return (0);
} else {
do {
endp--;
} while (endp > path && *endp == '/');
}
if (endp - path + 2 > PATH_MAX)
{
_rtld_error("Filename is too long: %s", path);
return(-1);
}
strncpy(bname, path, endp - path + 1);
bname[endp - path + 1] = '\0';
return (0);
}
static void
linkmap_add(Obj_Entry *obj)
{
struct link_map *l = &obj->linkmap;
struct link_map *prev;
obj->linkmap.l_name = obj->path;
obj->linkmap.l_addr = obj->mapbase;
obj->linkmap.l_ld = obj->dynamic;
#ifdef __mips__
/* GDB needs load offset on MIPS to use the symbols */
obj->linkmap.l_offs = obj->relocbase;
#endif
if (r_debug.r_map == NULL) {
r_debug.r_map = l;
return;
}
/*
* Scan to the end of the list, but not past the entry for the
* dynamic linker, which we want to keep at the very end.
*/
for (prev = r_debug.r_map;
prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
prev = prev->l_next)
;
/* Link in the new entry. */
l->l_prev = prev;
l->l_next = prev->l_next;
if (l->l_next != NULL)
l->l_next->l_prev = l;
prev->l_next = l;
}
static void
linkmap_delete(Obj_Entry *obj)
{
struct link_map *l = &obj->linkmap;
if (l->l_prev == NULL) {
if ((r_debug.r_map = l->l_next) != NULL)
l->l_next->l_prev = NULL;
return;
}
if ((l->l_prev->l_next = l->l_next) != NULL)
l->l_next->l_prev = l->l_prev;
}
/*
* Function for the debugger to set a breakpoint on to gain control.
*
* The two parameters allow the debugger to easily find and determine
* what the runtime loader is doing and to whom it is doing it.
*
* When the loadhook trap is hit (r_debug_state, set at program
* initialization), the arguments can be found on the stack:
*
* +8 struct link_map *m
* +4 struct r_debug *rd
* +0 RetAddr
*/
void
r_debug_state(struct r_debug* rd, struct link_map *m)
{
}
/*
* Get address of the pointer variable in the main program.
*/
static const void **
get_program_var_addr(const char *name)
{
const Obj_Entry *obj;
unsigned long hash;
hash = elf_hash(name);
for (obj = obj_main; obj != NULL; obj = obj->next) {
const Elf_Sym *def;
if ((def = symlook_obj(name, hash, obj, false)) != NULL) {
const void **addr;
addr = (const void **)(obj->relocbase + def->st_value);
return addr;
}
}
return NULL;
}
/*
* Set a pointer variable in the main program to the given value. This
* is used to set key variables such as "environ" before any of the
* init functions are called.
*/
static void
set_program_var(const char *name, const void *value)
{
const void **addr;
if ((addr = get_program_var_addr(name)) != NULL) {
dbg("\"%s\": *%p <-- %p", name, addr, value);
*addr = value;
}
}
/*
* Given a symbol name in a referencing object, find the corresponding
* definition of the symbol. Returns a pointer to the symbol, or NULL if
* no definition was found. Returns a pointer to the Obj_Entry of the
* defining object via the reference parameter DEFOBJ_OUT.
*/
static const Elf_Sym *
symlook_default(const char *name, unsigned long hash,
const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
{
DoneList donelist;
const Elf_Sym *def;
const Elf_Sym *symp;
const Obj_Entry *obj;
const Obj_Entry *defobj;
const Objlist_Entry *elm;
def = NULL;
defobj = NULL;
donelist_init(&donelist);
/* Look first in the referencing object if linked symbolically. */
if (refobj->symbolic && !donelist_check(&donelist, refobj)) {
symp = symlook_obj(name, hash, refobj, in_plt);
if (symp != NULL) {
def = symp;
defobj = refobj;
}
}
/* Search all objects loaded at program start up. */
if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
symp = symlook_list(name, hash, &list_main, &obj, in_plt, &donelist);
if (symp != NULL &&
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
def = symp;
defobj = obj;
}
}
/* Search all DAGs whose roots are RTLD_GLOBAL objects. */
STAILQ_FOREACH(elm, &list_global, link) {
if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
break;
symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
&donelist);
if (symp != NULL &&
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
def = symp;
defobj = obj;
}
}
/* Search all dlopened DAGs containing the referencing object. */
STAILQ_FOREACH(elm, &refobj->dldags, link) {
if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
break;
symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
&donelist);
if (symp != NULL &&
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
def = symp;
defobj = obj;
}
}
/*
* Search the dynamic linker itself, and possibly resolve the
* symbol from there. This is how the application links to
* dynamic linker services such as dlopen. Only the values listed
* in the "exports" array can be resolved from the dynamic linker.
*/
if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
symp = symlook_obj(name, hash, &obj_rtld, in_plt);
if (symp != NULL && is_exported(symp)) {
def = symp;
defobj = &obj_rtld;
}
}
if (def != NULL)
*defobj_out = defobj;
return def;
}
static const Elf_Sym *
symlook_list(const char *name, unsigned long hash, Objlist *objlist,
const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
{
const Elf_Sym *symp;
const Elf_Sym *def;
const Obj_Entry *defobj;
const Objlist_Entry *elm;
def = NULL;
defobj = NULL;
STAILQ_FOREACH(elm, objlist, link) {
if (donelist_check(dlp, elm->obj))
continue;
if ((symp = symlook_obj(name, hash, elm->obj, in_plt)) != NULL) {
if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
def = symp;
defobj = elm->obj;
if (ELF_ST_BIND(def->st_info) != STB_WEAK)
break;
}
}
}
if (def != NULL)
*defobj_out = defobj;
return def;
}
#endif /* 0 */
/*
* Search the symbol table of a single shared object for a symbol of
* the given name. Returns a pointer to the symbol, or NULL if no
* definition was found.
*
* The symbol's hash value is passed in for efficiency reasons; that
* eliminates many recomputations of the hash value.
*/
const Elf_Sym *
symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
bool in_plt)
{
if (obj->buckets != NULL) {
unsigned long symnum = obj->buckets[hash % obj->nbuckets];
while (symnum != STN_UNDEF) {
const Elf_Sym *symp;
const char *strp;
if (symnum >= obj->nchains)
return NULL; /* Bad object */
symp = obj->symtab + symnum;
strp = obj->strtab + symp->st_name;
if (name[0] == strp[0] && strcmp(name, strp) == 0)
return symp->st_shndx != SHN_UNDEF ||
(!in_plt && symp->st_value != 0 &&
ELF_ST_TYPE(symp->st_info) == STT_FUNC) ? symp : NULL;
symnum = obj->chains[symnum];
}
}
return NULL;
}
#if 0
static void
trace_loaded_objects(Obj_Entry *obj)
{
char *fmt1, *fmt2, *fmt, *main_local, *list_containers;
int c;
if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
main_local = "";
if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL)
fmt1 = "\t%o => %p (%x)\n";
if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL)
fmt2 = "\t%o (%x)\n";
list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL");
for (; obj; obj = obj->next) {
Needed_Entry *needed;
char *name, *path;
bool is_lib;
if (list_containers && obj->needed != NULL)
printf("%s:\n", obj->path);
for (needed = obj->needed; needed; needed = needed->next) {
if (needed->obj != NULL) {
if (needed->obj->traced && !list_containers)
continue;
needed->obj->traced = true;
path = needed->obj->path;
} else
path = "not found";
name = (char *)obj->strtab + needed->name;
is_lib = strncmp(name, "lib", 3) == 0; /* XXX - bogus */
fmt = is_lib ? fmt1 : fmt2;
while ((c = *fmt++) != '\0') {
switch (c) {
default:
putchar(c);
continue;
case '\\':
switch (c = *fmt) {
case '\0':
continue;
case 'n':
putchar('\n');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -