Skip to content

Fix static linking of mysql library to root4star#343

Closed
plexoos wants to merge 4 commits intomainfrom
pr/fix_static_mysql_ld
Closed

Fix static linking of mysql library to root4star#343
plexoos wants to merge 4 commits intomainfrom
pr/fix_static_mysql_ld

Conversation

@plexoos
Copy link
Copy Markdown
Member

@plexoos plexoos commented May 4, 2022

According to cons scripts, the entire mysqlclient library is requested to be linked statically to root4star using the following linker flags:

...
-Wl,--whole-archive -Wl,-Bstatic -Wl,-z -Wl,muldefs
`mysql_config --libs`
-Wl,--no-whole-archive -Wl,-Bdynamic
...

The linker is instructed to look for static versions of the libraries enclosed between the -Wl,--whole-archive and -Wl,--no-whole-archive options, however when such libs cannot be found it rightfully fails. Now, normally, mysql_config --libs is supposed to return something similar to this:

-L$pkglibdir -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl

But the mysql_config script on the SDCC machines was apparently modified to match the specific linker command in cons, so it returns additional flags:

$ diff /opt/star/sl73_gcc485/bin/mysql_config /opt/star/sl73_gcc485/bin/mysql_config~
110c110
< libs=" $ldflags -L$pkglibdir  -lmysqlclient   -L/usr/lib -Wl,--no-whole-archive -Wl,-Bdynamic -lpthread -lz -lm -lssl -lcrypto -ldl "
---
> libs=" $ldflags -L$pkglibdir  -lmysqlclient   -lpthread -lz -lm -lssl -lcrypto -ldl "
112c112
< libs_r=" $ldflags -L$pkglibdir   -lmysqlclient  -L/usr/lib -Wl,--no-whole-archive -Wl,-Bdynamic  -lpthread -lz -lm -lssl -lcrypto -ldl   "
---
> libs_r=" $ldflags -L$pkglibdir   -lmysqlclient   -lpthread -lz -lm -lssl -lcrypto -ldl   "
$ ls -la /opt/star/sl73_gcc485/bin/mysql_config /opt/star/sl73_gcc485/bin/mysql_config~
-rwxr-xr-x 1 jeromel rhstar 6842 Oct 10  2017 /opt/star/sl73_gcc485/bin/mysql_config
-rwxr-xr-x 1 jeromel rhstar 6746 Oct 10  2017 /opt/star/sl73_gcc485/bin/mysql_config~

The point of that manual intervention was obviously to exclude the non-existing static libraries following the -Wl,-Bdynamic option. Since modifying generated package configurations is never a good idea, this fix will allow to rely on standard mysql installations without any further patching of cons scripts.

plexoos added 2 commits May 4, 2022 18:34
According to cons scripts, the entire `mysqlclient` library is requested
to be linked statically to `root4star` using the following linker flags:

```
...
-Wl,--whole-archive -Wl,-Bstatic -Wl,-z -Wl,muldefs
`mysql_config --libs`
-Wl,--no-whole-archive -Wl,-Bdynamic
...
```

The linker is instructed to look for static versions of the libraries
enclosed between the `-Wl,--whole-archive` and `-Wl,--no-whole-archive`
options, however when such libs cannot be found it rightfully fails.
Now, normally, `mysql_config --libs` is supposed to return something
similar to this:

```
-L$pkglibdir -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
```

But the `mysql_config` script on the farm was apparently modified to
match the specific linker command in cons, so it returns additional
flags:

```diff
$ diff /opt/star/sl73_gcc485/bin/mysql_config /opt/star/sl73_gcc485/bin/mysql_config~
110c110
< libs=" $ldflags -L$pkglibdir  -lmysqlclient   -L/usr/lib -Wl,--no-whole-archive -Wl,-Bdynamic -lpthread -lz -lm -lssl -lcrypto -ldl "
---
> libs=" $ldflags -L$pkglibdir  -lmysqlclient   -lpthread -lz -lm -lssl -lcrypto -ldl "
112c112
< libs_r=" $ldflags -L$pkglibdir   -lmysqlclient  -L/usr/lib -Wl,--no-whole-archive -Wl,-Bdynamic  -lpthread -lz -lm -lssl -lcrypto -ldl   "
---
> libs_r=" $ldflags -L$pkglibdir   -lmysqlclient   -lpthread -lz -lm -lssl -lcrypto -ldl   "
```

```shell
$ ls -la /opt/star/sl73_gcc485/bin/mysql_config /opt/star/sl73_gcc485/bin/mysql_config~
-rwxr-xr-x 1 jeromel rhstar 6842 Oct 10  2017 /opt/star/sl73_gcc485/bin/mysql_config
-rwxr-xr-x 1 jeromel rhstar 6746 Oct 10  2017 /opt/star/sl73_gcc485/bin/mysql_config~
```

The point of this manual intervention was obviously to exclude the
non-existing static libraries following the `-Wl,-Bdynamic` option.
Since modifying generated package configurations is never a good idea,
this fix will allow to rely on standard mysql installations without
any further patching of cons scripts.
The default behavior of linking mysqlclient to root4star statically does
not seem to work with the unpatched library. Perhaps the requirement to
include all object files is not really necessary `-Wl,--whole-archive
-Wl,-Bstatic` and can be relaxed. Nevertheless, we can give the users an
option to link the library dynamically by setting the environment
variable `STAR_MYSQL_LINKSO`
@plexoos plexoos changed the title Fix static linking of mysql library to root4star Make static linking of mysql library to root4star optional May 5, 2022
@plexoos plexoos changed the title Make static linking of mysql library to root4star optional Fix static linking of mysql library to root4star May 5, 2022
@plexoos plexoos marked this pull request as ready for review May 5, 2022 20:15
@perevbnlgov
Copy link
Copy Markdown
Contributor

Actually in root.exe it is working with shared library. but if you preferes to load statically, it is not a big deal

@perevbnlgov perevbnlgov closed this May 5, 2022
@plexoos plexoos reopened this May 5, 2022
@veprbl
Copy link
Copy Markdown
Member

veprbl commented May 5, 2022

Last time I did dive into this rabbit hole and root4star was broken with shared linking.

@plexoos
Copy link
Copy Markdown
Member Author

plexoos commented May 5, 2022

Shared linking is how we were doing it in the containers from the beginning. Static linking did not work in the container for the reason described above. The existing tests work with mysqlclient.so and now after the fix. What's broken then?

@veprbl
Copy link
Copy Markdown
Member

veprbl commented May 5, 2022

My tests were with modern binutils and gcc. I would need to revisit it to say exactly, but I think the shared libraries loaded as part of the chains would fail to find symbols linked into the executable.

Comment thread asps/rexe/MAIN_rmain.cxx
// A dummy global definition to satisfy the linker when linking mysqlclient library statically with --whole-archive
// /opt/software/linux-scientific7-x86_64/gcc-4.8.5/mysql-5.7.27-pfyt3fwtkubcc5eazmoqfick3lgp67mf/lib/libmysqlclient.a(posix_timers.c.o): In function `my_timer_initialize':
// (.text+0x140): undefined reference to `key_thread_timer_notifier'
unsigned int key_thread_timer_notifier = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to find this symbol. Maybe try:

find /opt -name "*.a" -exec nm {} \; | grep key_thread_timer_notifier

Copy link
Copy Markdown
Member Author

@plexoos plexoos May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I saw it defined in libmysqld.a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you only see this issue after we strip the -lpthread -lz -lm -lssl -lcrypto -ldl?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or when we strip -L$pkglibdir?

@plexoos
Copy link
Copy Markdown
Member Author

plexoos commented May 6, 2022

My tests were with modern binutils and gcc. I would need to revisit it to say exactly, but I think the shared libraries loaded as part of the chains would fail to find symbols linked into the executable.

What you say makes perfect sense, but is it really relevant to this specific fix dealing with libmysqlclient only? With this change we will bring the build process in the container even closer to how it is done in the SDCC environment.

@veprbl
Copy link
Copy Markdown
Member

veprbl commented May 6, 2022

What you say makes perfect sense, but is it really relevant to this specific fix dealing with libmysqlclient only? With this change we will bring the build process in the container even closer to how it is done in the SDCC environment.

Yeah, seems like my issue is not relevant to this specific PR. I'm looking forward to having this reviewed and merged. Enabling shared linking, if it somehow works, would be even nicer.

@plexoos
Copy link
Copy Markdown
Member Author

plexoos commented May 6, 2022

I fully support switching to shared linking as it would be much cleaner. It is also trivial since all it takes is just applying the patch we have hard coded in the Dockerfiles. My only worry is that someone may have a script not ready for the switch...

plexoos added a commit that referenced this pull request May 6, 2022
An alternative solution to the issue described in PR #343
plexoos added a commit that referenced this pull request May 12, 2022
An alternative solution to the issue described in PR #343
@plexoos
Copy link
Copy Markdown
Member Author

plexoos commented May 13, 2022

Superseded by #346

@plexoos plexoos closed this May 13, 2022
@plexoos plexoos deleted the pr/fix_static_mysql_ld branch May 13, 2022 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants