Delay setting linker soname until post-reloc and post-ctor

Setting the linker's soname ("ld-android.so") can allocate heap memory
now that the name uses an std::string, and it's probably a good idea to
defer doing this until after the linker has relocated itself (and after
it has called C++ constructors for global variables.)

Bug: none
Test: bionic unit tests
Test: verify that dlopen("ld-android.so", RTLD_NOLOAD) works
Change-Id: I6b9bd7552c3ae9b77e3ee9e2a98b069b8eef25ca
This commit is contained in:
Ryan Prichard 2021-01-12 23:09:10 -08:00
parent b4fd072976
commit bb1e37358f
3 changed files with 26 additions and 18 deletions

View File

@ -328,7 +328,7 @@ soinfo* get_libdl_info(const soinfo& linker_si) {
__libdl_info->ref_count_ = 1; __libdl_info->ref_count_ = 1;
__libdl_info->strtab_size_ = linker_si.strtab_size_; __libdl_info->strtab_size_ = linker_si.strtab_size_;
__libdl_info->local_group_root_ = __libdl_info; __libdl_info->local_group_root_ = __libdl_info;
__libdl_info->soname_ = linker_si.soname_.c_str(); __libdl_info->soname_ = linker_si.soname_;
__libdl_info->target_sdk_version_ = __ANDROID_API__; __libdl_info->target_sdk_version_ = __ANDROID_API__;
__libdl_info->generate_handle(); __libdl_info->generate_handle();
#if defined(__work_around_b_24465209__) #if defined(__work_around_b_24465209__)

View File

@ -3193,26 +3193,27 @@ bool soinfo::prelink_image() {
return false; return false;
} }
// second pass - parse entries relying on strtab // Second pass - parse entries relying on strtab. Skip this while relocating the linker so as to
for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) { // avoid doing heap allocations until later in the linker's initialization.
switch (d->d_tag) { if (!relocating_linker) {
case DT_SONAME: for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
set_soname(get_string(d->d_un.d_val)); switch (d->d_tag) {
break; case DT_SONAME:
case DT_RUNPATH: set_soname(get_string(d->d_un.d_val));
set_dt_runpath(get_string(d->d_un.d_val)); break;
break; case DT_RUNPATH:
set_dt_runpath(get_string(d->d_un.d_val));
break;
}
} }
} }
// Before M release linker was using basename in place of soname. // Before M release, linker was using basename in place of soname. In the case when DT_SONAME is
// In the case when dt_soname is absent some apps stop working // absent some apps stop working because they can't find DT_NEEDED library by soname. This
// because they can't find dt_needed library by soname. // workaround should keep them working. (Applies only for apps targeting sdk version < M.) Make
// This workaround should keep them working. (Applies only // an exception for the main executable, which does not need to have DT_SONAME. The linker has an
// for apps targeting sdk version < M.) Make an exception for // DT_SONAME but the soname_ field is initialized later on.
// the main executable and linker; they do not need to have dt_soname. if (soname_.empty() && this != solist_get_somain() && !relocating_linker &&
// TODO: >= O the linker doesn't need this workaround.
if (soname_.empty() && this != solist_get_somain() && (flags_ & FLAG_LINKER) == 0 &&
get_application_target_sdk_version() < 23) { get_application_target_sdk_version() < 23) {
soname_ = basename(realpath_.c_str()); soname_ = basename(realpath_.c_str());
DL_WARN_documented_change(23, "missing-soname-enforced-for-api-level-23", DL_WARN_documented_change(23, "missing-soname-enforced-for-api-level-23",

View File

@ -725,6 +725,13 @@ __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so)
// Initialize the linker's own global variables // Initialize the linker's own global variables
tmp_linker_so.call_constructors(); tmp_linker_so.call_constructors();
// Setting the linker soinfo's soname can allocate heap memory, so delay it until here.
for (const ElfW(Dyn)* d = tmp_linker_so.dynamic; d->d_tag != DT_NULL; ++d) {
if (d->d_tag == DT_SONAME) {
tmp_linker_so.set_soname(tmp_linker_so.get_string(d->d_un.d_val));
}
}
// When the linker is run directly rather than acting as PT_INTERP, parse // When the linker is run directly rather than acting as PT_INTERP, parse
// arguments and determine the executable to load. When it's instead acting // arguments and determine the executable to load. When it's instead acting
// as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the