提交 5552ce52 编写于 作者: R Rich Felker

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.
上级 16f70388
...@@ -22,6 +22,8 @@ BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS))) ...@@ -22,6 +22,8 @@ BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS)))
ARCH_SRCS = $(wildcard $(srcdir)/src/*/$(ARCH)/*.[csS]) ARCH_SRCS = $(wildcard $(srcdir)/src/*/$(ARCH)/*.[csS])
ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS))) ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS)))
REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS))) 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)))) OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS))))
LOBJS = $(OBJS:.o=.lo) LOBJS = $(OBJS:.o=.lo)
GENH = obj/include/bits/alltypes.h GENH = obj/include/bits/alltypes.h
...@@ -72,7 +74,7 @@ endif ...@@ -72,7 +74,7 @@ endif
all: $(ALL_LIBS) $(ALL_TOOLS) 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) $(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) ...@@ -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/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 obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC
...@@ -117,12 +119,12 @@ NOSSP_SRCS = $(wildcard crt/*.c) \ ...@@ -117,12 +119,12 @@ NOSSP_SRCS = $(wildcard crt/*.c) \
src/env/__libc_start_main.c src/env/__init_tls.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/thread/__set_thread_area.c src/env/__stack_chk_fail.c \
src/string/memset.c src/string/memcpy.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) $(NOSSP_SRCS:%.c=obj/%.o) $(NOSSP_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
$(CRT_LIBS:lib/%=obj/crt/%): CFLAGS_ALL += -DCRT $(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 $@ $< CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<
...@@ -151,10 +153,10 @@ obj/%.lo: $(srcdir)/%.S ...@@ -151,10 +153,10 @@ obj/%.lo: $(srcdir)/%.S
obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH) obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH)
$(CC_CMD) $(CC_CMD)
lib/libc.so: $(LOBJS) lib/libc.so: $(LOBJS) $(LDSO_OBJS)
$(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \ $(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \
-Wl,-e,_dlstart -Wl,-Bsymbolic-functions \ -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
-o $@ $(LOBJS) $(LIBCC) -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
lib/libc.a: $(OBJS) lib/libc.a: $(OBJS)
rm -f $@ rm -f $@
......
#define SHARED #define SHARED
#define START "_start" #define START "_start"
#define _dlstart_c _start_c #define _dlstart_c _start_c
#include "../src/ldso/dlstart.c" #include "../ldso/dlstart.c"
int main(); int main();
void _init() __attribute__((weak)); void _init() __attribute__((weak));
......
#include <stddef.h> #include <stddef.h>
#include "dynlink.h" #include "dynlink.h"
#ifdef SHARED
#ifndef START #ifndef START
#define START "_dlstart" #define START "_dlstart"
#endif #endif
...@@ -146,5 +144,3 @@ void _dlstart_c(size_t *sp, size_t *dynv) ...@@ -146,5 +144,3 @@ void _dlstart_c(size_t *sp, size_t *dynv)
GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]); GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
dls2((void *)base, sp); dls2((void *)base, sp);
} }
#endif
#ifdef SHARED
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -1930,4 +1929,3 @@ static void error(const char *fmt, ...) ...@@ -1930,4 +1929,3 @@ static void error(const char *fmt, ...)
__dl_vseterr(fmt, ap); __dl_vseterr(fmt, ap);
va_end(ap); va_end(ap);
} }
#endif
#ifdef SHARED
#include <stddef.h> #include <stddef.h>
#include "libc.h" #include "libc.h"
...@@ -12,5 +10,3 @@ ptrdiff_t __tlsdesc_static() ...@@ -12,5 +10,3 @@ ptrdiff_t __tlsdesc_static()
} }
weak_alias(__tlsdesc_static, __tlsdesc_dynamic); weak_alias(__tlsdesc_static, __tlsdesc_dynamic);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册