Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Kevin
bionic
Commits
2f41531f
Commit
2f41531f
authored
12 years ago
by
Elliott Hughes
Committed by
Gerrit Code Review
12 years ago
Browse files
Options
Download
Plain Diff
Merge "Clean up the argc/argv/envp/auxv handling."
parents
d4187efd
42b2c6a5
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
384 additions
and
291 deletions
+384
-291
libc/Android.mk
libc/Android.mk
+4
-4
libc/arch-arm/bionic/crtbegin.c
libc/arch-arm/bionic/crtbegin.c
+7
-23
libc/arch-x86/bionic/crtbegin.c
libc/arch-x86/bionic/crtbegin.c
+7
-22
libc/bionic/getauxval.cpp
libc/bionic/getauxval.cpp
+2
-6
libc/bionic/libc_init_common.cpp
libc/bionic/libc_init_common.cpp
+20
-30
libc/bionic/libc_init_common.h
libc/bionic/libc_init_common.h
+21
-8
libc/bionic/libc_init_dynamic.cpp
libc/bionic/libc_init_dynamic.cpp
+115
-0
libc/bionic/libc_init_static.cpp
libc/bionic/libc_init_static.cpp
+44
-61
libc/private/KernelArgumentBlock.h
libc/private/KernelArgumentBlock.h
+76
-0
libc/private/bionic_tls.h
libc/private/bionic_tls.h
+1
-1
libc/stdlib/atexit.h
libc/stdlib/atexit.h
+4
-0
linker/arch/x86/begin.c
linker/arch/x86/begin.c
+7
-8
linker/linker.cpp
linker/linker.cpp
+58
-99
linker/linker_environ.cpp
linker/linker_environ.cpp
+13
-24
linker/linker_environ.h
linker/linker_environ.h
+5
-5
No files found.
libc/Android.mk
View file @
2f41531f
...
...
@@ -177,7 +177,6 @@ libc_common_src_files := \
bionic/isatty.c
\
bionic/issetugid.c
\
bionic/ldexp.c
\
bionic/libc_init_common.c
\
bionic/logd_write.c
\
bionic/lseek64.c
\
bionic/md5.c
\
...
...
@@ -275,6 +274,7 @@ libc_bionic_src_files := \
bionic/__fgets_chk.cpp
\
bionic/getauxval.cpp
\
bionic/getcwd.cpp
\
bionic/libc_init_common.cpp
\
bionic/libgen.cpp
\
bionic/__memcpy_chk.cpp
\
bionic/__memmove_chk.cpp
\
...
...
@@ -824,7 +824,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES
:=
\
$(libc_arch_static_src_files)
\
$(libc_static_common_src_files)
\
bionic/libc_init_static.c
bionic/libc_init_static.c
pp
LOCAL_C_INCLUDES
:=
$(libc_common_c_includes)
LOCAL_CFLAGS
:=
$(libc_common_cflags)
\
...
...
@@ -849,7 +849,7 @@ LOCAL_SRC_FILES := \
$(libc_static_common_src_files)
\
bionic/dlmalloc.c
\
bionic/malloc_debug_common.cpp
\
bionic/libc_init_static.c
bionic/libc_init_static.c
pp
LOCAL_CFLAGS
:=
$(libc_common_cflags)
\
-DLIBC_STATIC
\
...
...
@@ -883,7 +883,7 @@ LOCAL_SRC_FILES := \
bionic/dlmalloc.c
\
bionic/malloc_debug_common.cpp
\
bionic/pthread_debug.cpp
\
bionic/libc_init_dynamic.c
bionic/libc_init_dynamic.c
pp
ifeq
($(TARGET_ARCH),arm)
LOCAL_NO_CRT
:=
true
...
...
This diff is collapsed.
Click to expand it.
libc/arch-arm/bionic/crtbegin.c
View file @
2f41531f
...
...
@@ -26,21 +26,8 @@
* SUCH DAMAGE.
*/
typedef
struct
{
void
(
**
preinit_array
)(
void
);
void
(
**
init_array
)(
void
);
void
(
**
fini_array
)(
void
);
}
structors_array_t
;
extern
int
main
(
int
argc
,
char
**
argv
,
char
**
env
);
extern
void
__libc_init
(
unsigned
int
*
elfdata
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
);
#include "../../bionic/libc_init_common.h"
#include <stddef.h>
__attribute__
((
section
(
".preinit_array"
)))
void
(
*
__PREINIT_ARRAY__
)(
void
)
=
(
void
(
*
)(
void
))
-
1
;
...
...
@@ -51,17 +38,14 @@ void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
__attribute__
((
section
(
".fini_array"
)))
void
(
*
__FINI_ARRAY__
)(
void
)
=
(
void
(
*
)(
void
))
-
1
;
__attribute__
((
visibility
(
"hidden"
)))
void
_start
()
{
__LIBC_HIDDEN__
void
_start
()
{
structors_array_t
array
;
void
*
elfdata
;
array
.
preinit_array
=
&
__PREINIT_ARRAY__
;
array
.
init_array
=
&
__INIT_ARRAY__
;
array
.
fini_array
=
&
__FINI_ARRAY__
;
array
.
init_array
=
&
__INIT_ARRAY__
;
array
.
fini_array
=
&
__FINI_ARRAY__
;
elfdata
=
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
);
__libc_init
(
elfdata
,
(
void
*
)
0
,
&
main
,
&
array
);
void
*
raw_args
=
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
);
__libc_init
(
raw_args
,
NULL
,
&
main
,
&
array
);
}
#include "__dso_handle.h"
...
...
This diff is collapsed.
Click to expand it.
libc/arch-x86/bionic/crtbegin.c
View file @
2f41531f
...
...
@@ -26,21 +26,8 @@
* SUCH DAMAGE.
*/
typedef
struct
{
void
(
**
preinit_array
)(
void
);
void
(
**
init_array
)(
void
);
void
(
**
fini_array
)(
void
);
}
structors_array_t
;
extern
int
main
(
int
argc
,
char
**
argv
,
char
**
env
);
extern
void
__libc_init
(
unsigned
int
*
elfdata
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
);
#include "../../bionic/libc_init_common.h"
#include <stddef.h>
__attribute__
((
section
(
".preinit_array"
)))
void
(
*
__PREINIT_ARRAY__
)(
void
)
=
(
void
(
*
)(
void
))
-
1
;
...
...
@@ -51,18 +38,16 @@ void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
__attribute__
((
section
(
".fini_array"
)))
void
(
*
__FINI_ARRAY__
)(
void
)
=
(
void
(
*
)(
void
))
-
1
;
__
attribute__
((
visibility
(
"hidden"
)))
__
LIBC_HIDDEN__
__attribute__
((
force_align_arg_pointer
))
void
_start
()
{
structors_array_t
array
;
void
*
elfdata
;
array
.
preinit_array
=
&
__PREINIT_ARRAY__
;
array
.
init_array
=
&
__INIT_ARRAY__
;
array
.
fini_array
=
&
__FINI_ARRAY__
;
array
.
init_array
=
&
__INIT_ARRAY__
;
array
.
fini_array
=
&
__FINI_ARRAY__
;
elfdata
=
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
);
__libc_init
(
elfdata
,
(
void
*
)
0
,
&
main
,
&
array
);
void
*
raw_args
=
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
);
__libc_init
(
raw_args
,
NULL
,
&
main
,
&
array
);
}
#include "__dso_handle.h"
...
...
This diff is collapsed.
Click to expand it.
libc/bionic/getauxval.cpp
View file @
2f41531f
...
...
@@ -32,17 +32,13 @@
#include <private/bionic_auxv.h>
#include <elf.h>
__LIBC_HIDDEN__
Elf32_auxv_t
*
__libc_auxv
=
NULL
;
__LIBC_HIDDEN__
Elf32_auxv_t
*
__libc_auxv
=
NULL
;
extern
"C"
unsigned
long
int
getauxval
(
unsigned
long
int
type
)
{
Elf32_auxv_t
*
v
;
for
(
v
=
__libc_auxv
;
v
->
a_type
!=
AT_NULL
;
v
++
)
{
for
(
Elf32_auxv_t
*
v
=
__libc_auxv
;
v
->
a_type
!=
AT_NULL
;
++
v
)
{
if
(
v
->
a_type
==
type
)
{
return
v
->
a_un
.
a_val
;
}
}
return
0
;
}
This diff is collapsed.
Click to expand it.
libc/bionic/libc_init_common.c
→
libc/bionic/libc_init_common.c
pp
View file @
2f41531f
...
...
@@ -30,29 +30,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <elf.h>
#include <asm/page.h>
#include "pthread_internal.h"
#include "atexit.h"
#include "KernelArgumentBlock.h"
#include "libc_init_common.h"
#include <bionic_tls.h>
#include <errno.h>
#include <private/bionic_auxv.h>
extern
unsigned
__get_sp
(
void
);
extern
pid_t
gettid
(
void
);
extern
"C"
unsigned
__get_sp
(
void
);
extern
"C"
int
__system_properties_init
(
void
);
char
*
__progname
;
// Not public, but well-known in the BSDs.
char
*
__progname
;
// Declared in <unistd.h>
char
**
environ
;
/
* from
asm/page.h
*/
/
/ Declared in <
asm/page.h
>.
unsigned
int
__page_size
=
PAGE_SIZE
;
unsigned
int
__page_shift
=
PAGE_SHIFT
;
int
__system_properties_init
(
void
);
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
* in memory. Beware: all writes to libc globals from this function will
* apply to linker-private copies and will not be visible from libc later on.
...
...
@@ -61,10 +63,10 @@ int __system_properties_init(void);
* stores the pointer in TLS, but does not add it to pthread's gThreadList. This
* has to be done later from libc itself (see __libc_init_common).
*
* This function also stores
the elf_data argument in a specific
TLS slot to be
later
* This function also stores
a pointer to the kernel argument block in a
TLS slot to be
* picked up by the libc constructor.
*/
void
__libc_init_tls
(
unsigned
**
elf_data
)
{
extern
"C"
void
__libc_init_tls
(
void
*
kernel_argument_block
)
{
unsigned
stack_top
=
(
__get_sp
()
&
~
(
PAGE_SIZE
-
1
))
+
PAGE_SIZE
;
unsigned
stack_size
=
128
*
1024
;
unsigned
stack_bottom
=
stack_top
-
stack_size
;
...
...
@@ -78,33 +80,21 @@ void __libc_init_tls(unsigned** elf_data) {
static
void
*
tls_area
[
BIONIC_TLS_SLOTS
];
__init_tls
(
tls_area
,
&
thread
);
tls_area
[
TLS_SLOT_BIONIC_PREINIT
]
=
elf_data
;
tls_area
[
TLS_SLOT_BIONIC_PREINIT
]
=
kernel_argument_block
;
}
void
__libc_init_common
(
uintptr_t
*
elf_data
)
{
int
argc
=
*
elf_data
;
char
**
argv
=
(
char
**
)
(
elf_data
+
1
);
char
**
envp
=
argv
+
argc
+
1
;
void
__libc_init_common
(
KernelArgumentBlock
&
args
)
{
// Initialize various globals.
environ
=
args
.
envp
;
errno
=
0
;
__libc_auxv
=
args
.
auxv
;
__progname
=
args
.
argv
[
0
]
?
args
.
argv
[
0
]
:
const_cast
<
char
*>
(
"<unknown>"
);
// Get the main thread from TLS and add it to the thread list.
pthread_internal_t
*
main_thread
=
__get_thread
();
main_thread
->
allocated_on_heap
=
false
;
_pthread_internal_add
(
main_thread
);
// Set various globals.
errno
=
0
;
__progname
=
argv
[
0
]
?
argv
[
0
]
:
"<unknown>"
;
environ
=
envp
;
// The auxiliary vector is at the end of the environment block
while
(
*
envp
!=
NULL
)
{
envp
++
;
}
/* The end of the environment block is marked by two NULL pointers */
envp
++
;
__libc_auxv
=
(
Elf32_auxv_t
*
)
envp
;
__system_properties_init
();
// Requires 'environ'.
}
...
...
@@ -116,7 +106,7 @@ void __libc_init_common(uintptr_t* elf_data) {
* entry in the list has value -1, the last one has value 0.
*/
void
__libc_fini
(
void
*
array
)
{
void
**
fini_array
=
array
;
void
**
fini_array
=
reinterpret_cast
<
void
**>
(
array
)
;
const
size_t
minus1
=
~
(
size_t
)
0
;
/* ensure proper sign extension */
/* Sanity check - first entry must be -1 */
...
...
@@ -135,7 +125,7 @@ void __libc_fini(void* array) {
/* Now call each destructor in reverse order. */
while
(
count
>
0
)
{
void
(
*
func
)()
=
(
void
(
*
))
fini_array
[
--
count
];
void
(
*
func
)()
=
(
void
(
*
)
()
)
fini_array
[
--
count
];
/* Sanity check, any -1 in the list is ignored */
if
((
size_t
)
func
==
minus1
)
{
...
...
This diff is collapsed.
Click to expand it.
libc/bionic/libc_init_common.h
View file @
2f41531f
...
...
@@ -28,16 +28,29 @@
#ifndef LIBC_INIT_COMMON_H
#define LIBC_INIT_COMMON_H
#include <s
tdint
.h>
#include <s
ys/cdefs
.h>
typedef
struct
{
void
(
**
preinit_array
)(
void
);
void
(
**
init_array
)(
void
);
void
(
**
fini_array
)(
void
);
typedef
struct
{
void
(
**
preinit_array
)(
void
);
void
(
**
init_array
)(
void
);
void
(
**
fini_array
)(
void
);
}
structors_array_t
;
extern
void
__libc_init_common
(
uintptr_t
*
elfdata
);
extern
void
__libc_fini
(
void
*
finit_array
);
__BEGIN_DECLS
extern
int
main
(
int
argc
,
char
**
argv
,
char
**
env
);
__noreturn
void
__libc_init
(
void
*
raw_args
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
);
void
__libc_fini
(
void
*
finit_array
);
__END_DECLS
#if defined(__cplusplus)
struct
KernelArgumentBlock
;
void
__libc_init_common
(
KernelArgumentBlock
&
args
);
#endif
#endif
This diff is collapsed.
Click to expand it.
libc/bionic/libc_init_dynamic.c
→
libc/bionic/libc_init_dynamic.c
pp
View file @
2f41531f
...
...
@@ -49,79 +49,67 @@
#include <stdint.h>
#include <elf.h>
#include "atexit.h"
#include "KernelArgumentBlock.h"
#include "libc_init_common.h"
#include <bionic_tls.h>
/* We flag the __libc_preinit function as a constructor to ensure
* that its address is listed in libc.so's .init_array section.
* This ensures that the function is called by the dynamic linker
* as soon as the shared library is loaded.
*/
void
__attribute__
((
constructor
))
__libc_preinit
(
void
);
extern
"C"
{
extern
void
pthread_debug_init
(
void
);
extern
void
malloc_debug_init
(
void
);
extern
void
malloc_debug_fini
(
void
);
};
void
__libc_preinit
(
void
)
{
/* Read the ELF data pointer from a special slot of the
* TLS area, then call __libc_init_common with it.
*
* Note that:
* - we clear the slot so no other initializer sees its value.
* - __libc_init_common() will change the TLS area so the old one
* won't be accessible anyway.
*/
void
**
tls_area
=
(
void
**
)
__get_tls
();
unsigned
*
elfdata
=
tls_area
[
TLS_SLOT_BIONIC_PREINIT
];
// We flag the __libc_preinit function as a constructor to ensure
// that its address is listed in libc.so's .init_array section.
// This ensures that the function is called by the dynamic linker
// as soon as the shared library is loaded.
void
__attribute__
((
constructor
))
__libc_preinit
(
void
);
tls_area
[
TLS_SLOT_BIONIC_PREINIT
]
=
NULL
;
void
__libc_preinit
()
{
// Read the kernel argument block pointer from TLS.
void
*
tls
=
const_cast
<
void
*>
(
__get_tls
());
KernelArgumentBlock
**
args_slot
=
&
reinterpret_cast
<
KernelArgumentBlock
**>
(
tls
)[
TLS_SLOT_BIONIC_PREINIT
];
KernelArgumentBlock
*
args
=
*
args_slot
;
__libc_init_common
(
elfdata
);
// Clear the slot so no other initializer sees its value.
// __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
*
args_slot
=
NULL
;
/* Setup pthread routines accordingly to the environment.
* Requires system properties
*/
extern
void
pthread_debug_init
(
void
);
pthread_debug_init
();
__libc_init_common
(
*
args
);
/* Setup malloc routines accordingly to the environment.
* Requires system properties
*/
extern
void
malloc_debug_init
(
void
);
malloc_debug_init
();
// Hooks for the debug malloc and pthread libraries to let them know that we're starting up.
pthread_debug_init
();
malloc_debug_init
();
}
void
__libc_postfini
(
void
)
{
extern
void
malloc_debug_fini
(
void
);
malloc_debug_fini
();
void
__libc_postfini
()
{
// A hook for the debug malloc library to let it know that we're shutting down.
malloc_debug_fini
();
}
/* This function is called from the executable's _start entry point
* (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
* called by the dynamic linker after it has loaded all shared
* libraries the executable depends on.
*
* Note that the dynamic linker has also run all constructors in the
* executable at this point.
*/
__noreturn
void
__libc_init
(
uintptr_t
*
elfdata
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
)
{
int
argc
=
(
int
)
*
elfdata
;
char
**
argv
=
(
char
**
)(
elfdata
+
1
);
char
**
envp
=
argv
+
argc
+
1
;
// This function is called from the executable's _start entry point
// (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
// called by the dynamic linker after it has loaded all shared
// libraries the executable depends on.
//
// Note that the dynamic linker has also run all constructors in the
// executable at this point.
__noreturn
void
__libc_init
(
void
*
raw_args
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
)
{
KernelArgumentBlock
args
(
raw_args
);
/* Several Linux ABIs don't pass the onexit pointer, and the ones that
* do never use it. Therefore, we ignore it.
*/
// Several Linux ABIs don't pass the onexit pointer, and the ones that
// do never use it. Therefore, we ignore it.
/
*
The executable may have its own destructors listed in its .fini_array
*
so we need to ensure that these are called when the program exits
*
normally.
*/
if
(
structors
->
fini_array
)
__cxa_atexit
(
__libc_fini
,
structors
->
fini_array
,
NULL
);
/
/
The executable may have its own destructors listed in its .fini_array
//
so we need to ensure that these are called when the program exits
//
normally.
if
(
structors
->
fini_array
)
{
__cxa_atexit
(
__libc_fini
,
structors
->
fini_array
,
NULL
);
}
exit
(
slingshot
(
argc
,
arg
v
,
envp
));
exit
(
slingshot
(
args
.
argc
,
arg
s
.
argv
,
args
.
envp
));
}
This diff is collapsed.
Click to expand it.
libc/bionic/libc_init_static.c
→
libc/bionic/libc_init_static.c
pp
View file @
2f41531f
...
...
@@ -37,19 +37,20 @@
* arrays that must be run before the program's 'main' routine is launched.
*/
#include <elf.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <s
tdint
.h>
#include <
elf
.h>
#include "pthread_internal.h"
#include <s
ys/auxv
.h>
#include <
sys/mman
.h>
#include "atexit.h"
#include "bionic_tls.h"
#include "KernelArgumentBlock.h"
#include "libc_init_common.h"
#include <bionic_tls.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/auxv.h>
#include "pthread_internal.h"
// Returns the address of the page containing address 'x'.
#define PAGE_START(x) ((x) & PAGE_MASK)
...
...
@@ -58,71 +59,53 @@
// itself at the start of a page.
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
static
void
call_array
(
void
(
**
list
)())
{
// First element is -1, list is null-terminated
while
(
*++
list
)
{
(
*
list
)();
}
static
void
call_array
(
void
(
**
list
)())
{
// First element is -1, list is null-terminated
while
(
*++
list
)
{
(
*
list
)();
}
}
static
void
apply_gnu_relro
()
{
Elf32_Phdr
*
phdr_start
;
unsigned
long
int
phdr_ct
;
Elf32_Phdr
*
phdr
;
phdr_start
=
(
Elf32_Phdr
*
)
getauxval
(
AT_PHDR
);
phdr_ct
=
getauxval
(
AT_PHNUM
);
Elf32_Phdr
*
phdr_start
=
reinterpret_cast
<
Elf32_Phdr
*>
(
getauxval
(
AT_PHDR
));
unsigned
long
int
phdr_ct
=
getauxval
(
AT_PHNUM
);
for
(
phdr
=
phdr_start
;
phdr
<
(
phdr_start
+
phdr_ct
);
phdr
++
)
{
if
(
phdr
->
p_type
!=
PT_GNU_RELRO
)
continue
;
Elf32_Addr
seg_page_start
=
PAGE_START
(
phdr
->
p_vaddr
);
Elf32_Addr
seg_page_end
=
PAGE_END
(
phdr
->
p_vaddr
+
phdr
->
p_memsz
);
// Check return value here? What do we do if we fail?
mprotect
((
void
*
)
seg_page_start
,
seg_page_end
-
seg_page_start
,
PROT_READ
);
for
(
Elf32_Phdr
*
phdr
=
phdr_start
;
phdr
<
(
phdr_start
+
phdr_ct
);
phdr
++
)
{
if
(
phdr
->
p_type
!=
PT_GNU_RELRO
)
{
continue
;
}
}
__noreturn
void
__libc_init
(
uintptr_t
*
elfdata
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
)
{
int
argc
;
char
**
argv
,
**
envp
;
Elf32_Addr
seg_page_start
=
PAGE_START
(
phdr
->
p_vaddr
);
Elf32_Addr
seg_page_end
=
PAGE_END
(
phdr
->
p_vaddr
+
phdr
->
p_memsz
);
__libc_init_tls
(
NULL
);
/* Initialize the C runtime environment */
__libc_init_common
(
elfdata
);
// Check return value here? What do we do if we fail?
mprotect
(
reinterpret_cast
<
void
*>
(
seg_page_start
),
seg_page_end
-
seg_page_start
,
PROT_READ
);
}
}
apply_gnu_relro
();
__noreturn
void
__libc_init
(
void
*
raw_args
,
void
(
*
onexit
)(
void
),
int
(
*
slingshot
)(
int
,
char
**
,
char
**
),
structors_array_t
const
*
const
structors
)
{
__libc_init_tls
(
NULL
);
/* Several Linux ABIs don't pass the onexit pointer, and the ones that
* do never use it. Therefore, we ignore it.
*/
KernelArgumentBlock
args
(
raw_args
);
__libc_init_common
(
args
);
/* pre-init array. */
call_array
(
structors
->
preinit_array
);
apply_gnu_relro
();
//
call static constructors
call_array
(
structors
->
init_array
);
//
Several Linux ABIs don't pass the onexit pointer, and the ones that
// do never use it. Therefore, we ignore it.
argc
=
(
int
)
*
elfdata
;
argv
=
(
char
**
)(
elfdata
+
1
);
envp
=
argv
+
argc
+
1
;
call_array
(
structors
->
preinit_array
);
call_array
(
structors
->
init_array
);
/
*
The executable may have its own destructors listed in its .fini_array
*
so we need to ensure that these are called when the program exits
*
normally.
*/
if
(
structors
->
fini_array
)
__cxa_atexit
(
__libc_fini
,
structors
->
fini_array
,
NULL
);
/
/
The executable may have its own destructors listed in its .fini_array
//
so we need to ensure that these are called when the program exits
//
normally.
if
(
structors
->
fini_array
!=
NULL
)
{
__cxa_atexit
(
__libc_fini
,
structors
->
fini_array
,
NULL
);
}
exit
(
slingshot
(
argc
,
arg
v
,
envp
));
exit
(
slingshot
(
args
.
argc
,
arg
s
.
argv
,
args
.
envp
));
}
This diff is collapsed.
Click to expand it.
libc/private/KernelArgumentBlock.h
0 → 100644
View file @
2f41531f
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef KERNEL_ARGUMENT_BLOCK_H
#define KERNEL_ARGUMENT_BLOCK_H
#include <elf.h>
#include <stdint.h>
#include <sys/auxv.h>
// When the kernel starts the dynamic linker, it passes a pointer to a block
// of memory containing argc, the argv array, the environment variable array,
// and the array of ELF aux vectors. This class breaks that block up into its
// constituents for easy access.
class
KernelArgumentBlock
{
public:
KernelArgumentBlock
(
void
*
raw_args
)
{
uint32_t
*
args
=
reinterpret_cast
<
uint32_t
*>
(
raw_args
);
argc
=
static_cast
<
int
>
(
*
args
);
argv
=
reinterpret_cast
<
char
**>
(
args
+
1
);
envp
=
argv
+
argc
+
1
;
// Skip over all environment variable definitions to find aux vector.
// The end of the environment block is marked by two NULL pointers.
char
**
p
=
envp
;
while
(
*
p
!=
NULL
)
{
++
p
;
}
++
p
;
// Skip second NULL;
auxv
=
reinterpret_cast
<
Elf32_auxv_t
*>
(
p
);
}
// Similar to ::getauxval but doesn't require the libc global variables to be set up,
// so it's safe to call this really early on. This function also lets you distinguish
// between the inability to find the given type and its value just happening to be 0.
unsigned
long
getauxval
(
unsigned
long
type
,
bool
*
found_match
=
NULL
)
{
for
(
Elf32_auxv_t
*
v
=
auxv
;
v
->
a_type
!=
AT_NULL
;
++
v
)
{
if
(
v
->
a_type
==
type
)
{
if
(
found_match
!=
NULL
)
{
*
found_match
=
true
;
}
return
v
->
a_un
.
a_val
;
}
}
if
(
found_match
!=
NULL
)
{
*
found_match
=
false
;
}
return
0
;
}
int
argc
;
char
**
argv
;
char
**
envp
;
Elf32_auxv_t
*
auxv
;
private:
// Disallow copy and assignment.
KernelArgumentBlock
(
const
KernelArgumentBlock
&
);
void
operator
=
(
const
KernelArgumentBlock
&
);
};
#endif // KERNEL_ARGUMENT_BLOCK_H
This diff is collapsed.
Click to expand it.
libc/private/bionic_tls.h
View file @
2f41531f
...
...
@@ -134,7 +134,7 @@ extern void* __get_tls( void );
extern
void
*
__get_stack_base
(
int
*
p_stack_size
);
/* Initialize the TLS. */
extern
void
__libc_init_tls
(
unsigned
**
elfdata
);
extern
void
__libc_init_tls
(
void
*
kernel_argument_block
);
__END_DECLS
...
...
This diff is collapsed.
Click to expand it.
libc/stdlib/atexit.h
View file @
2f41531f
...
...
@@ -44,8 +44,12 @@ struct atexit {
}
fns
[
1
];
/* the table itself */
};
__BEGIN_DECLS
extern
int
__atexit_invalid
;
extern
struct
atexit
*
__atexit
;
/* points to head of LIFO stack */
int
__cxa_atexit
(
void
(
*
)(
void
*
),
void
*
,
void
*
);
void
__cxa_finalize
(
void
*
);
__END_DECLS
This diff is collapsed.
Click to expand it.
linker/arch/x86/begin.c
View file @
2f41531f
...
...
@@ -26,24 +26,23 @@
* SUCH DAMAGE.
*/
#include <s
tdint
.h>
#include <s
ys/cdefs
.h>
extern
unsigned
__linker_init
(
unsigned
int
*
elfdata
);
extern
unsigned
__linker_init
(
void
*
raw_args
);
__attribute__
((
visibility
(
"hidden"
)))
void
_start
()
{
__LIBC_HIDDEN__
void
_start
()
{
void
(
*
start
)(
void
);
void
*
elfdata
=
(
void
*
)
((
uintptr_t
)
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
));
start
=
(
void
(
*
)(
void
))
__linker_init
(
elfdata
);
void
*
raw_args
=
(
void
*
)
((
uintptr_t
)
__builtin_frame_address
(
0
)
+
sizeof
(
void
*
));
start
=
(
void
(
*
)(
void
))
__linker_init
(
raw_args
);
/* linker init returns (%eax) the _entry address in the main image */
/* entry point expects sp to point to
elfdata
*/
/* entry point expects sp to point to
raw_args
*/
__asm__
(
"mov %0, %%esp
\n\t
"
"jmp *%1
\n\t
"
:
:
"r"
(
elfdata
),
"r"
(
start
)
:
:
:
"r"
(
raw_args
),
"r"
(
start
)
:
);
/* Unreachable */
...
...
This diff is collapsed.
Click to expand it.
linker/linker.cpp
View file @
2f41531f
...
...
@@ -42,6 +42,7 @@
// Private C library headers.
#include <private/bionic_tls.h>
#include <private/debug_format.h>
#include <private/KernelArgumentBlock.h>
#include <private/logd.h>
#include <private/ScopedPthreadMutexLocker.h>
...
...
@@ -1771,15 +1772,8 @@ static bool soinfo_link_image(soinfo* si) {
* fixed it's own GOT. It is safe to make references to externs
* and other non-local data at this point.
*/
static
unsigned
__linker_init_post_relocation
(
unsigned
**
elfdata
,
unsigned
linker_base
)
{
static
soinfo
linker_soinfo
;
int
argc
=
(
int
)
*
elfdata
;
char
**
argv
=
(
char
**
)
(
elfdata
+
1
);
unsigned
*
vecs
=
(
unsigned
*
)
(
argv
+
argc
+
1
);
/* NOTE: we store the elfdata pointer on a special location
static
unsigned
__linker_init_post_relocation
(
KernelArgumentBlock
&
args
,
unsigned
linker_base
)
{
/* NOTE: we store the args pointer on a special location
* of the temporary TLS area in order to pass it to
* the C Library's runtime initializer.
*
...
...
@@ -1787,7 +1781,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
* to point to a different location to ensure that no other
* shared library constructor can access it.
*/
__libc_init_tls
(
elfdata
);
__libc_init_tls
(
&
args
);
#if TIMING
struct
timeval
t0
,
t1
;
...
...
@@ -1795,7 +1789,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
#endif
// Initialize environment functions, and get to the ELF aux vectors table.
vecs
=
linker_env_init
(
vec
s
);
linker_env_init
(
arg
s
);
debugger_init
();
...
...
@@ -1815,9 +1809,8 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
}
INFO
(
"[ android linker & debugger ]
\n
"
);
DEBUG
(
"elfdata @ 0x%08x
\n
"
,
(
unsigned
)
elfdata
);
soinfo
*
si
=
soinfo_alloc
(
argv
[
0
]);
soinfo
*
si
=
soinfo_alloc
(
args
.
argv
[
0
]);
if
(
si
==
NULL
)
{
exit
(
EXIT_FAILURE
);
}
...
...
@@ -1827,7 +1820,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
link_map
*
map
=
&
(
si
->
linkmap
);
map
->
l_addr
=
0
;
map
->
l_name
=
argv
[
0
];
map
->
l_name
=
args
.
argv
[
0
];
map
->
l_prev
=
NULL
;
map
->
l_next
=
NULL
;
...
...
@@ -1841,9 +1834,11 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
* Don't use soinfo_alloc(), because the linker shouldn't
* be on the soinfo list.
*/
strlcpy
((
char
*
)
linker_soinfo
.
name
,
"/system/bin/linker"
,
sizeof
linker_soinfo
.
name
);
static
soinfo
linker_soinfo
;
strlcpy
(
linker_soinfo
.
name
,
"/system/bin/linker"
,
sizeof
(
linker_soinfo
.
name
));
linker_soinfo
.
flags
=
0
;
linker_soinfo
.
base
=
linker_base
;
/*
* Set the dynamic field in the link map otherwise gdb will complain with
* the following:
...
...
@@ -1851,42 +1846,29 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
* expected address (wrong library or version mismatch?)
*/
Elf32_Ehdr
*
elf_hdr
=
(
Elf32_Ehdr
*
)
linker_base
;
Elf32_Phdr
*
phdr
=
(
Elf32_Phdr
*
)((
unsigned
char
*
)
linker_base
+
elf_hdr
->
e_phoff
);
Elf32_Phdr
*
phdr
=
(
Elf32_Phdr
*
)((
unsigned
char
*
)
linker_base
+
elf_hdr
->
e_phoff
);
phdr_table_get_dynamic_section
(
phdr
,
elf_hdr
->
e_phnum
,
linker_base
,
&
linker_soinfo
.
dynamic
,
NULL
,
NULL
);
insert_soinfo_into_debug_map
(
&
linker_soinfo
);
/* extract information passed from the kernel */
while
(
vecs
[
0
]
!=
0
){
switch
(
vecs
[
0
]){
case
AT_PHDR
:
si
->
phdr
=
(
Elf32_Phdr
*
)
vecs
[
1
];
break
;
case
AT_PHNUM
:
si
->
phnum
=
(
int
)
vecs
[
1
];
break
;
case
AT_ENTRY
:
si
->
entry
=
vecs
[
1
];
break
;
}
vecs
+=
2
;
}
// Extract information passed from the kernel.
si
->
phdr
=
reinterpret_cast
<
Elf32_Phdr
*>
(
args
.
getauxval
(
AT_PHDR
));
si
->
phnum
=
args
.
getauxval
(
AT_PHNUM
);
si
->
entry
=
args
.
getauxval
(
AT_ENTRY
);
/* Compute the value of si->base. We can't rely on the fact that
* the first entry is the PHDR because this will not be true
* for certain executables (e.g. some in the NDK unit test suite)
*/
int
nn
;
si
->
base
=
0
;
si
->
size
=
phdr_table_get_load_size
(
si
->
phdr
,
si
->
phnum
);
si
->
load_bias
=
0
;
for
(
nn
=
0
;
nn
<
si
->
phnum
;
nn
++
)
{
if
(
si
->
phdr
[
nn
].
p_type
==
PT_PHDR
)
{
si
->
load_bias
=
(
Elf32_Addr
)
si
->
phdr
-
si
->
phdr
[
nn
].
p_vaddr
;
si
->
base
=
(
Elf32_Addr
)
si
->
phdr
-
si
->
phdr
[
nn
].
p_offset
;
break
;
}
for
(
int
i
=
0
;
i
<
si
->
phnum
;
++
i
)
{
if
(
si
->
phdr
[
i
].
p_type
==
PT_PHDR
)
{
si
->
load_bias
=
reinterpret_cast
<
Elf32_Addr
>
(
si
->
phdr
)
-
si
->
phdr
[
i
].
p_vaddr
;
si
->
base
=
reinterpret_cast
<
Elf32_Addr
>
(
si
->
phdr
)
-
si
->
phdr
[
i
].
p_offset
;
break
;
}
}
si
->
dynamic
=
(
unsigned
*
)
-
1
;
si
->
refcount
=
1
;
...
...
@@ -1920,13 +1902,13 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
#if TIMING
gettimeofday
(
&
t1
,
NULL
);
PRINT
(
"LINKER TIME: %s: %d microseconds
\n
"
,
argv
[
0
],
(
int
)
(
PRINT
(
"LINKER TIME: %s: %d microseconds
\n
"
,
e
.
argv
[
0
],
(
int
)
(
(((
long
long
)
t1
.
tv_sec
*
1000000LL
)
+
(
long
long
)
t1
.
tv_usec
)
-
(((
long
long
)
t0
.
tv_sec
*
1000000LL
)
+
(
long
long
)
t0
.
tv_usec
)
));
#endif
#if STATS
PRINT
(
"RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol
\n
"
,
argv
[
0
],
PRINT
(
"RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol
\n
"
,
e
.
argv
[
0
],
linker_stats
.
count
[
kRelocAbsolute
],
linker_stats
.
count
[
kRelocRelative
],
linker_stats
.
count
[
kRelocCopy
],
...
...
@@ -1946,7 +1928,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
}
}
}
PRINT
(
"PAGES MODIFIED: %s: %d (%dKB)
\n
"
,
argv
[
0
],
count
,
count
*
4
);
PRINT
(
"PAGES MODIFIED: %s: %d (%dKB)
\n
"
,
e
.
argv
[
0
],
count
,
count
*
4
);
}
#endif
...
...
@@ -1958,31 +1940,6 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
return
si
->
entry
;
}
/*
* Find the value of AT_BASE passed to us by the kernel. This is the load
* location of the linker.
*/
static
unsigned
find_linker_base
(
unsigned
**
elfdata
)
{
int
argc
=
(
int
)
*
elfdata
;
char
**
argv
=
(
char
**
)
(
elfdata
+
1
);
unsigned
*
vecs
=
(
unsigned
*
)
(
argv
+
argc
+
1
);
while
(
vecs
[
0
]
!=
0
)
{
vecs
++
;
}
/* The end of the environment block is marked by two NULL pointers */
vecs
++
;
while
(
vecs
[
0
])
{
if
(
vecs
[
0
]
==
AT_BASE
)
{
return
vecs
[
1
];
}
vecs
+=
2
;
}
return
0
;
// should never happen
}
/* Compute the load-bias of an existing executable. This shall only
* be used to compute the load bias of an executable or shared library
* that was loaded by the kernel itself.
...
...
@@ -2018,39 +1975,41 @@ get_elf_exec_load_bias(const Elf32_Ehdr* elf)
* relocations, any attempt to reference an extern variable, extern
* function, or other GOT reference will generate a segfault.
*/
extern
"C"
unsigned
__linker_init
(
unsigned
**
elfdata
)
{
unsigned
linker_addr
=
find_linker_base
(
elfdata
);
Elf32_Ehdr
*
elf_hdr
=
(
Elf32_Ehdr
*
)
linker_addr
;
Elf32_Phdr
*
phdr
=
(
Elf32_Phdr
*
)((
unsigned
char
*
)
linker_addr
+
elf_hdr
->
e_phoff
);
soinfo
linker_so
;
memset
(
&
linker_so
,
0
,
sizeof
(
soinfo
));
linker_so
.
base
=
linker_addr
;
linker_so
.
size
=
phdr_table_get_load_size
(
phdr
,
elf_hdr
->
e_phnum
);
linker_so
.
load_bias
=
get_elf_exec_load_bias
(
elf_hdr
);
linker_so
.
dynamic
=
(
unsigned
*
)
-
1
;
linker_so
.
phdr
=
phdr
;
linker_so
.
phnum
=
elf_hdr
->
e_phnum
;
linker_so
.
flags
|=
FLAG_LINKER
;
if
(
!
soinfo_link_image
(
&
linker_so
))
{
// It would be nice to print an error message, but if the linker
// can't link itself, there's no guarantee that we'll be able to
// call write() (because it involves a GOT reference).
//
// This situation should never occur unless the linker itself
// is corrupt.
exit
(
EXIT_FAILURE
);
}
extern
"C"
unsigned
__linker_init
(
void
*
raw_args
)
{
KernelArgumentBlock
args
(
raw_args
);
unsigned
linker_addr
=
args
.
getauxval
(
AT_BASE
);
Elf32_Ehdr
*
elf_hdr
=
(
Elf32_Ehdr
*
)
linker_addr
;
Elf32_Phdr
*
phdr
=
(
Elf32_Phdr
*
)((
unsigned
char
*
)
linker_addr
+
elf_hdr
->
e_phoff
);
soinfo
linker_so
;
memset
(
&
linker_so
,
0
,
sizeof
(
soinfo
));
linker_so
.
base
=
linker_addr
;
linker_so
.
size
=
phdr_table_get_load_size
(
phdr
,
elf_hdr
->
e_phnum
);
linker_so
.
load_bias
=
get_elf_exec_load_bias
(
elf_hdr
);
linker_so
.
dynamic
=
(
unsigned
*
)
-
1
;
linker_so
.
phdr
=
phdr
;
linker_so
.
phnum
=
elf_hdr
->
e_phnum
;
linker_so
.
flags
|=
FLAG_LINKER
;
if
(
!
soinfo_link_image
(
&
linker_so
))
{
// It would be nice to print an error message, but if the linker
// can't link itself, there's no guarantee that we'll be able to
// call write() (because it involves a GOT reference).
//
// This situation should never occur unless the linker itself
// is corrupt.
exit
(
EXIT_FAILURE
);
}
// We have successfully fixed our own relocations. It's safe to run
// the main part of the linker now.
unsigned
start_address
=
__linker_init_post_relocation
(
elfdata
,
linker_addr
);
// We have successfully fixed our own relocations. It's safe to run
// the main part of the linker now.
unsigned
start_address
=
__linker_init_post_relocation
(
args
,
linker_addr
);
set_soinfo_pool_protection
(
PROT_READ
);
set_soinfo_pool_protection
(
PROT_READ
);
// Return the address that the calling assembly stub should jump to.
return
start_address
;
// Return the address that the calling assembly stub should jump to.
return
start_address
;
}
This diff is collapsed.
Click to expand it.
linker/linker_environ.cpp
View file @
2f41531f
...
...
@@ -33,6 +33,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <private/KernelArgumentBlock.h>
static
char
**
_envp
;
static
bool
_AT_SECURE_value
=
true
;
...
...
@@ -40,21 +42,18 @@ bool get_AT_SECURE() {
return
_AT_SECURE_value
;
}
static
void
__init_AT_SECURE
(
unsigned
*
auxv
)
{
static
void
__init_AT_SECURE
(
KernelArgumentBlock
&
args
)
{
// Check auxv for AT_SECURE first to see if program is setuid, setgid,
// has file caps, or caused a SELinux/AppArmor domain transition.
for
(
unsigned
*
v
=
auxv
;
v
[
0
];
v
+=
2
)
{
if
(
v
[
0
]
==
AT_SECURE
)
{
// Kernel told us whether to enable secure mode.
_AT_SECURE_value
=
v
[
1
];
return
;
}
}
bool
kernel_supplied_AT_SECURE
;
_AT_SECURE_value
=
args
.
getauxval
(
AT_SECURE
,
&
kernel_supplied_AT_SECURE
);
// We don't support ancient kernels.
const
char
*
msg
=
"FATAL: kernel did not supply AT_SECURE
\n
"
;
write
(
2
,
msg
,
strlen
(
msg
));
exit
(
EXIT_FAILURE
);
if
(
!
kernel_supplied_AT_SECURE
)
{
const
char
*
msg
=
"FATAL: kernel did not supply AT_SECURE
\n
"
;
write
(
2
,
msg
,
strlen
(
msg
));
exit
(
EXIT_FAILURE
);
}
}
// Check if the environment variable definition at 'envstr'
...
...
@@ -164,22 +163,12 @@ static void __sanitize_environment_variables() {
dst
[
0
]
=
NULL
;
}
unsigned
*
linker_env_init
(
unsigned
*
environment_and_aux_vector
s
)
{
void
linker_env_init
(
KernelArgumentBlock
&
arg
s
)
{
// Store environment pointer - can't be NULL.
_envp
=
reinterpret_cast
<
char
**>
(
environment_and_aux_vectors
)
;
_envp
=
args
.
envp
;
// Skip over all environment variable definitions.
// The end of the environment block is marked by two NULL pointers.
unsigned
*
aux_vectors
=
environment_and_aux_vectors
;
while
(
aux_vectors
[
0
]
!=
0
)
{
++
aux_vectors
;
}
++
aux_vectors
;
__init_AT_SECURE
(
aux_vectors
);
__init_AT_SECURE
(
args
);
__sanitize_environment_variables
();
return
aux_vectors
;
}
const
char
*
linker_env_get
(
const
char
*
name
)
{
...
...
This diff is collapsed.
Click to expand it.
linker/linker_environ.h
View file @
2f41531f
...
...
@@ -29,10 +29,10 @@
#ifndef LINKER_ENVIRON_H
#define LINKER_ENVIRON_H
// Call this function before anything else. 'environment_and_aux_vectors'
// must point to the environment block in the ELF data block. The function
//
returns the start of the aux vect
or
s
a
fter the environment block
.
extern
unsigned
*
linker_env_init
(
unsigned
*
environment_and_aux_vector
s
);
struct
KernelArgumentBlock
;
//
Call this function bef
or
e
a
ny of the other functions in this header file
.
extern
void
linker_env_init
(
KernelArgumentBlock
&
arg
s
);
// Returns the value of environment variable 'name' if defined and not
// empty, or NULL otherwise.
...
...
@@ -41,4 +41,4 @@ extern const char* linker_env_get(const char* name);
// Returns the value of this program's AT_SECURE variable.
extern
bool
get_AT_SECURE
();
#endif
/
*
LINKER_ENVIRON_H
*/
#endif /
/
LINKER_ENVIRON_H
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment