Skip to content

Commit

Permalink
Merge pull request #200 from jmglogow/fix_version_globbing
Browse files Browse the repository at this point in the history
[ELF] Fix '?' for globbing + version scripts
  • Loading branch information
rui314 authored Dec 26, 2021
2 parents d116113 + 31b0248 commit 1228bf3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion elf/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ std::string glob_to_regex(std::string_view pattern) {
switch (c) {
case '.': case '[': case ']': case '^':
case '$': case '\\': case '(': case ')':
case '+': case '?': case '|':
case '+': case '|':
ss << "\\" << c;
break;
case '*':
ss << ".*";
break;
case '?':
ss << ".";
break;
default:
ss << c;
break;
Expand Down
3 changes: 2 additions & 1 deletion elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ void apply_version_script(Context<E> &ctx) {
for (VersionPattern &elem : ctx.arg.version_patterns) {
std::vector<std::string_view> vec;

std::regex re_glob("[*?]", std::regex_constants::optimize | std::regex_constants::nosubs);
for (std::string_view pat : elem.patterns) {
if (pat.find('*') == pat.npos) {
if (!std::regex_search(pat.begin(), pat.end(), re_glob)) {
Symbol<E> *sym = intern(ctx, pat);
if (sym->file && !sym->file->is_dso)
sym->ver_idx = elem.ver_idx;
Expand Down
46 changes: 46 additions & 0 deletions test/elf/version-script8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
export LANG=
set -e
cd $(dirname $0)
mold=`pwd`/../../mold
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/../../out/test/elf/$(basename -s .sh $0)
mkdir -p $t

cat <<EOF > $t/a.ver
ver1 {
global: ?oo;
local: *;
};
ver2 {
global: b?r;
};
EOF

cat <<EOF | clang -fuse-ld=$mold -xc -shared -o $t/b.so -Wl,-version-script,$t/a.ver -
void foo() {}
void bar() {}
void baz() {}
EOF

cat <<EOF | clang -xc -c -o $t/c.o -
void foo();
void bar();
int main() {
foo();
bar();
return 0;
}
EOF

clang -fuse-ld=$mold -o $t/exe $t/c.o $t/b.so
$t/exe

readelf --dyn-syms $t/b.so > $t/log
fgrep -q 'foo@@ver1' $t/log
fgrep -q 'bar@@ver2' $t/log
! fgrep -q 'baz' $t/log || false

echo OK

0 comments on commit 1228bf3

Please sign in to comment.