move dynamic linker to its own top-level directory, ldso

this eliminates the last need for the SHARED macro to control how
files in the src tree are compiled. the same code is used for both
libc.a and libc.so, with additional code for the dynamic linker (from
the new ldso tree) being added to libc.so but not libc.a. separate .o
and .lo object files still exist for the src tree, but the only
difference is that the .lo files are built as PIC.

in the future, if/when we add dlopen support for static-linked
programs, much of the code in dynlink.c may be moved back into the src
tree, but properly factored into separate source files. in that case,
the code in the ldso tree will be reduced to just the dynamic linker
entry point, self-relocation, and loading of libraries needed by the
main application.
This commit is contained in:
Rich Felker 2016-01-25 19:29:55 -05:00
parent 16f70388d4
commit 5552ce5200
5 changed files with 11 additions and 19 deletions

View File

@ -22,6 +22,8 @@ BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS)))
ARCH_SRCS = $(wildcard $(srcdir)/src/*/$(ARCH)/*.[csS])
ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS)))
REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS)))
LDSO_SRCS = $(sort $(wildcard $(srcdir)/ldso/*.c))
LDSO_OBJS = $(patsubst $(srcdir)/%,obj/%.lo,$(basename $(LDSO_SRCS)))
OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS))))
LOBJS = $(OBJS:.o=.lo)
GENH = obj/include/bits/alltypes.h
@ -72,7 +74,7 @@ endif
all: $(ALL_LIBS) $(ALL_TOOLS)
OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(OBJS) $(GENH) $(GENH_INT))) $(addprefix obj/, crt crt/$(ARCH) include))
OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(OBJS) $(LDSO_OBJS) $(GENH) $(GENH_INT))) $(addprefix obj/, crt crt/$(ARCH) include))
$(ALL_LIBS) $(ALL_TOOLS) $(CRT_LIBS:lib/%=obj/crt/%) $(OBJS) $(LOBJS) $(GENH) $(GENH_INT): | $(OBJ_DIRS)
@ -95,11 +97,11 @@ obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git)
obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h
obj/crt/rcrt1.o obj/src/ldso/dlstart.lo obj/src/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h
obj/crt/rcrt1.o obj/ldso/dlstart.lo obj/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h
obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/src/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h
obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h
obj/crt/rcrt1.o: $(srcdir)/src/ldso/dlstart.c
obj/crt/rcrt1.o: $(srcdir)/ldso/dlstart.c
obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC
@ -117,12 +119,12 @@ NOSSP_SRCS = $(wildcard crt/*.c) \
src/env/__libc_start_main.c src/env/__init_tls.c \
src/thread/__set_thread_area.c src/env/__stack_chk_fail.c \
src/string/memset.c src/string/memcpy.c \
src/ldso/dlstart.c src/ldso/dynlink.c
ldso/dlstart.c ldso/dynlink.c
$(NOSSP_SRCS:%.c=obj/%.o) $(NOSSP_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
$(CRT_LIBS:lib/%=obj/crt/%): CFLAGS_ALL += -DCRT
$(LOBJS): CFLAGS_ALL += -fPIC -DSHARED
$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<
@ -151,10 +153,10 @@ obj/%.lo: $(srcdir)/%.S
obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH)
$(CC_CMD)
lib/libc.so: $(LOBJS)
lib/libc.so: $(LOBJS) $(LDSO_OBJS)
$(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \
-Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
-o $@ $(LOBJS) $(LIBCC)
-o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
lib/libc.a: $(OBJS)
rm -f $@

View File

@ -1,7 +1,7 @@
#define SHARED
#define START "_start"
#define _dlstart_c _start_c
#include "../src/ldso/dlstart.c"
#include "../ldso/dlstart.c"
int main();
void _init() __attribute__((weak));

View File

@ -1,8 +1,6 @@
#include <stddef.h>
#include "dynlink.h"
#ifdef SHARED
#ifndef START
#define START "_dlstart"
#endif
@ -146,5 +144,3 @@ void _dlstart_c(size_t *sp, size_t *dynv)
GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
dls2((void *)base, sp);
}
#endif

View File

@ -1,4 +1,3 @@
#ifdef SHARED
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
@ -1930,4 +1929,3 @@ static void error(const char *fmt, ...)
__dl_vseterr(fmt, ap);
va_end(ap);
}
#endif

View File

@ -1,5 +1,3 @@
#ifdef SHARED
#include <stddef.h>
#include "libc.h"
@ -12,5 +10,3 @@ ptrdiff_t __tlsdesc_static()
}
weak_alias(__tlsdesc_static, __tlsdesc_dynamic);
#endif