[PATCH] D125095: [Clang][AIX] Add .ref in frontend for AIX XCOFF to support `-bcdtors:csect` linker option

Ting Wang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 5 20:21:45 PDT 2022


tingwang added a comment.

In D125095#3552451 <https://reviews.llvm.org/D125095#3552451>, @shchenz wrote:

> Thanks for doing this. I am not familiar with the frontend, so I may be wrong/stupid in the follow comments : )
> Hope other experts like @hubert.reinterpretcast can give more meaningful comments.
>
> I tested on AIX, seems for static variable `static int x = foo();` in global scope, even compile with `-bcdtors:csect`, the init function also will not be eliminated. Could you please give an example to show why we need the new associated metadata for this case? Thanks.

Here is one example to show:

TEST_FOLDER=/tmp/test
mkdir -p $TEST_FOLDER
cd $TEST_FOLDER
cat > libbar.cc <<EOF
int globalVar = 2;
extern "C" int puts(const char *);
int foo() {

  puts("foo");
  globalVar = 42;
  return 30;

}
static int x = foo();
EOF

cat > libbaz.cc <<EOF
extern "C" int puts(const char *);
template <typename = void>
struct A {

  ~A() { puts("struct A ~A() 2"); }
  static A instance;

};

template <typename T> A<T> A<T>::instance;
void *zap() { return &A<>::instance; }

EOF

cat > uselib.cc <<EOF
#include <dlfcn.h>

int main(void) {

  void *handle = dlopen("./libbaz.so", RTLD_NOW | RTLD_LOCAL);
  dlclose(handle);

}
EOF

g++
===

XLC=g++
$XLC -fno-exceptions -c libbar.cc
rm -f libbar.a
ar qs libbar.a libbar.o
ranlib libbar.a
$XLC -fno-exceptions -c libbaz.cc
$XLC -shared -o libbaz.so libbaz.o libbar.a
$XLC -fno-exceptions -ldl uselib.cc -o uselib -ldl
./uselib 
foo
struct A ~A() 2

XLC 16.1.0
==========

XLC=/gsa/rtpgsa/projects/x/xlcmpbld/run/vacpp/16.1.0/aix/daily/191109/bin/xlclang++
$XLC -g -qnoeh -qfuncsect -c libbar.cc
rm -f libbar.a
ar qs libbar.a libbar.o
ranlib libbar.a
$XLC -g -qnoeh -qfuncsect -c libbaz.cc
$XLC -g -qtwolink -G -o libbaz.so libbaz.o libbar.a
$XLC -g -qnoeh uselib.cc -o uselib
./uselib 
foo
struct A ~A() 2

clang++ baseline
================

XLC=/home/tingwa/repo/llvm-project-base/dev/build/bin/clang++
$XLC -g -fignore-exceptions -ffunction-sections -c libbar.cc
rm -f libbar.a
ar qs libbar.a libbar.o
ranlib libbar.a
$XLC -g -fignore-exceptions -ffunction-sections -c libbaz.cc
$XLC -g -bcdtors:csect -shared -Wl,-G -o libbaz.so libbaz.o libbar.a
$XLC -g -fignore-exceptions uselib.cc -o uselib
./uselib
struct A ~A() 2

clang++ .ref
============

XLC=/home/tingwa/repo/llvm-project-12514-BE/dev/build/bin/clang++
$XLC -g -fignore-exceptions -ffunction-sections -c libbar.cc
rm -f libbar.a
ar qs libbar.a libbar.o
ranlib libbar.a
$XLC -g -fignore-exceptions -ffunction-sections -c libbaz.cc
$XLC -g -bcdtors:csect -shared -Wl,-G -o libbaz.so libbaz.o libbar.a
$XLC -g -fignore-exceptions uselib.cc -o uselib
./uselib 
foo
struct A ~A() 2

As shown in this example: without .ref association, clang++ baseline case will be wrong, since globalVar is updated in the init function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125095/new/

https://reviews.llvm.org/D125095



More information about the cfe-commits mailing list