https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92572
Bug ID: 92572
Summary: Vague linkage does not work reliably when a matching
segment is in a dynamically linked libarary on Linux
Product: gcc
Version: 8.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: wkaras at yahoo dot com
Target Milestone: ---
Here is an example:
----------
(conanrunenv) [root@d761696b8abf VagueDynLink]# bash -x x.sh
+ cat foo.h
#pragma once
inline int foo()
{
static int static_local;
return ++static_local;
}
+ cat lib.cc
#include "foo.h"
extern "C" void lib();
void lib()
{
static_cast<void>(foo());
}
+ cat main.cc
#include <cstdlib>
#include <iostream>
#include <dlfcn.h>
#include "foo.h"
int main()
{
void *lib_handle = dlopen("./lib.a", RTLD_NOW);
if (!lib_handle) {
std::cout << "dlopen() failed: " << dlerror() << '\n';
std::exit(1);
}
void *func = dlsym(lib_handle, "lib");
if (func) {
reinterpret_cast<void (*)()>(func)();
} else {
const char *err = dlerror();
if (!err) {
std::cout << "lib() not found\n";
} else {
std::cout << "dlsym() fail: " << err << '\n';
}
}
// Should output 2.
//
std::cout << foo() << '\n';
dlclose(lib_handle);
return 0;
}
+ CMN_OPTS='-O2 -Wall -Wextra -pedantic -std=c++17 -fno-strict-aliasing
-fwrapv'
+ cc -O2 -Wall -Wextra -pedantic -std=c++17 -fno-strict-aliasing -fwrapv -DLIB
-fPIC -c lib.cc
+ cc -O2 -Wall -Wextra -pedantic -std=c++17 -fno-strict-aliasing -fwrapv
-shared lib.o -o lib.a
+ cc -O2 -Wall -Wextra -pedantic -std=c++17 -fno-strict-aliasing -fwrapv -c
main.cc
+ cc -O2 -Wall -Wextra -pedantic -std=c++17 -fno-strict-aliasing -fwrapv main.o
-lstdc++ -ldl -o main
+ ./main
1
+ rm -f lib.o main.o lib.a main
(conanrunenv) [root@d761696b8abf VagueDynLink]# cc --version
cc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(conanrunenv) [root@d761696b8abf VagueDynLink]# uname -a
Linux d761696b8abf 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64
x86_64 x86_64 GNU/Linux
(conanrunenv) [root@d761696b8abf VagueDynLink]#
----------
The code is at https://github.com/ywkaras/MiscRepo/tree/master/VagueDynLink .