Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 题:改进利用链接符号名的做法 #194

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

frederick-vs-ja
Copy link
Contributor

这里改进的写法看上去不需要修改 ss,可能更“干净”一点。

原理:

  • Itanium ABI 上变量 ss::a 的重整名为 _ZN2ss1aE
  • 在 MSVC 上需要用 #pragma 告诉链接器使用 MSVC ABI 的重整名 ?a@ss@@3HA

Godbolt link

还有另一个作弊的写法,不过大概不符合要求:

#include<iostream>

namespace ss {
    int a = 0;
}

int main() {
    enum ss { a = 100 };
    std::cout << ss::a << '\n';
}

原理:
Itanium ABI 上变量 ss::a; 的重整名为 _ZN2ss1aE 。
在 MSVC 上需要用 pragma 告诉链接器使用 MSVC ABI 的重整名 ?a@ss@@3Ha
#ifdef _MSC_VER
#pragma comment(linker, "/alternatename:?_ZN2ss1aE@@3HA=?a@ss@@3HA")
#endif
_ZN2ss1aE = 100;
Copy link
Owner

Choose a reason for hiding this comment

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

我又学习了

@Mq-b
Copy link
Owner

Mq-b commented Dec 16, 2023

#include<iostream>

namespace ss {
    int a = 0;
}

int main() {
    enum ss { a = 100 };
    std::cout << ss::a << '\n';
}

这种利用名字查找优先级的方式的确不太合适。

@Mq-b Mq-b merged commit f7c957e into Mq-b:main Dec 16, 2023
@frederick-vs-ja frederick-vs-ja deleted the hack-with-mangled-names branch December 16, 2023 10:03
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.

2 participants