reduce code duplication in dynamic linker error paths

eventually this should help making dlerror thread-safe too.
This commit is contained in:
Rich Felker 2014-06-18 03:05:42 -04:00
parent adf94c1966
commit 7c73cacd09
1 changed files with 16 additions and 16 deletions

View File

@ -1,6 +1,7 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdint.h> #include <stdint.h>
@ -149,6 +150,17 @@ static int search_vec(size_t *v, size_t *r, size_t key)
return 1; return 1;
} }
static void error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(errbuf, sizeof errbuf, fmt, ap);
va_end(ap);
if (runtime) longjmp(*rtld_fail, 1);
dprintf(2, "%s\n", errbuf);
ldso_fail = 1;
}
static uint32_t sysv_hash(const char *s0) static uint32_t sysv_hash(const char *s0)
{ {
const unsigned char *s = (void *)s0; const unsigned char *s = (void *)s0;
@ -267,12 +279,9 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
if (!astype) continue; if (!astype) continue;
type = remap_rel(astype); type = remap_rel(astype);
if (!type) { if (!type) {
snprintf(errbuf, sizeof errbuf, error(errbuf, sizeof errbuf,
"Error relocating %s: unsupported relocation type %d", "Error relocating %s: unsupported relocation type %d",
dso->name, astype); dso->name, astype);
if (runtime) longjmp(*rtld_fail, 1);
dprintf(2, "%s\n", errbuf);
ldso_fail = 1;
continue; continue;
} }
sym_index = R_SYM(rel[1]); sym_index = R_SYM(rel[1]);
@ -284,12 +293,9 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
def = find_sym(ctx, name, type==REL_PLT); def = find_sym(ctx, name, type==REL_PLT);
if (!def.sym && (sym->st_shndx != SHN_UNDEF if (!def.sym && (sym->st_shndx != SHN_UNDEF
|| sym->st_info>>4 != STB_WEAK)) { || sym->st_info>>4 != STB_WEAK)) {
snprintf(errbuf, sizeof errbuf, error(errbuf, sizeof errbuf,
"Error relocating %s: %s: symbol not found", "Error relocating %s: %s: symbol not found",
dso->name, name); dso->name, name);
if (runtime) longjmp(*rtld_fail, 1);
dprintf(2, "%s\n", errbuf);
ldso_fail = 1;
continue; continue;
} }
} else { } else {
@ -798,12 +804,9 @@ static void load_deps(struct dso *p)
if (p->dynv[i] != DT_NEEDED) continue; if (p->dynv[i] != DT_NEEDED) continue;
dep = load_library(p->strings + p->dynv[i+1], p); dep = load_library(p->strings + p->dynv[i+1], p);
if (!dep) { if (!dep) {
snprintf(errbuf, sizeof errbuf, error(errbuf, sizeof errbuf,
"Error loading shared library %s: %m (needed by %s)", "Error loading shared library %s: %m (needed by %s)",
p->strings + p->dynv[i+1], p->name); p->strings + p->dynv[i+1], p->name);
if (runtime) longjmp(*rtld_fail, 1);
dprintf(2, "%s\n", errbuf);
ldso_fail = 1;
continue; continue;
} }
if (runtime) { if (runtime) {
@ -852,12 +855,9 @@ static void reloc_all(struct dso *p)
if (p->relro_start != p->relro_end && if (p->relro_start != p->relro_end &&
mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) { mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) {
snprintf(errbuf, sizeof errbuf, error(errbuf, sizeof errbuf,
"Error relocating %s: RELRO protection failed: %m", "Error relocating %s: RELRO protection failed: %m",
p->name); p->name);
if (runtime) longjmp(*rtld_fail, 1);
dprintf(2, "%s\n", errbuf);
ldso_fail = 1;
} }
p->relocated = 1; p->relocated = 1;