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

Linux: build on newer GCC and Clang releases, silence compiler warnings #216

Open
wants to merge 11 commits into
base: v2.57
Choose a base branch
from

Conversation

darealshinji
Copy link
Contributor

I've tried to fix some of the GCC compiler warnings on Linux. I've also silenced -Wdiscarded-qualifiers as there are way too many warnings, since the code doesn't seem to distinguish much between const char and char arrays.

By the way, I wish there was a make test Makefile target.

Here are the warnings from the unpatched sources (without qualifiers warnings):
https://gist.github.com/darealshinji/25fa279e5d765d78a006fc3180e9e2fb

@teoberi
Copy link

teoberi commented Oct 8, 2024

I cloned your v2.57-new branch and I'm trying to build with gcc version 14.2.0 (GCC) but it fail at the codegenv2.c file.

codegenv2.c: In function ‘GenerateInstrHash’:
codegenv2.c:93:21: error: passing argument 1 of ‘hash’ from incompatible pointer type [-Wincompatible-pointer-types]
93 | return hash(&hashBuffer, len);
| ^~~~~~~~~~~
| |
| uint_8 ()[32] {aka unsigned char ()[32]}
codegenv2.c:36:40: note: expected ‘const uint_8 ’ {aka ‘const unsigned char ’} but argument is of type ‘uint_8 ()[32]’ {aka ‘unsigned char ()[32]’}
36 | static unsigned int hash(const uint_8* data, int size)
| ~~~~~~~~~~~~~~^~~~
codegenv2.c: In function ‘CodeGenV2’:
codegenv2.c:1706:44: error: passing argument 7 of ‘BuildMemoryEncoding’ from incompatible pointer type [-Wincompatible-pointer-types]
1706 | &dispSize, &displacement, matchedInstr, opExpr, &needB, &needX, &needRR, CodeInfo);
| ^~~~~~~~~~~~~
| |
| union *
codegenv2.c:1226:42: note: expected ‘uint_64 ’ {aka ‘long unsigned int ’} but argument is of type ‘union
1226 | unsigned int
dispSize, uint_64
pDisp, struct Instr_Def
instr, struct expr opExpr[4], bool* needB,
| ~~~~~~~~~^~~~~
codegenv2.c:1714:54: error: passing argument 3 of ‘BuildVEX’ from incompatible pointer type [-Wincompatible-pointer-types]
1714 | BuildVEX(&needVEX, &vexSize, &vexBytes, matchedInstr, opExpr, needB, needX, opCount); /* Create the VEX prefix bytes for both reg and memory operands /
| ^~~~~~~~~
| |
| unsigned char (
)[3]
codegenv2.c:715:69: note: expected ‘unsigned char ’ but argument is of type ‘unsigned char ()[3]’
715 | void BuildVEX(bool* needVex, unsigned char* vexSize, unsigned char* vexBytes, struct Instr_Def* instr, struct expr opnd[4], bool needB, bool needX, uint_32 opCount)
| ~~~~~~~~~~~~~~~^~~~~~~~
make: *** [Makefile-Linux-GCC-64.mak:35: GccUnixR/codegenv2.o] Error 1

Your branch builds with gcc version 13.2.0 but here the error messages are just warnings.

@darealshinji
Copy link
Contributor Author

darealshinji commented Oct 8, 2024

I didn't change those parts in codegenv2.c. But I can try to fix that and get it to work on different versions of gcc and clang.

@teoberi
Copy link

teoberi commented Oct 8, 2024

Try building with GCC 14.2.
With your changes, the number of warnings has decreased significantly, but it still does not compile with this version of GCC.
If you have time to solve the problems in codegenv2.c it would be OK.

@darealshinji
Copy link
Contributor Author

It now builds on GCC 14 and Clang 18.

@teoberi
Copy link

teoberi commented Oct 9, 2024

Yes now it can be built.
I only modified one line in Makefile-Linux-GCC-64.mak to add the hardening options.
sed -i "s|CC = gcc|CC = gcc -fcommon -fstack-protector-strong -D_FORTIFY_SOURCE=3 -pie -fPIE -Wl,-z,now|g" Makefile-Linux-GCC-64.mak

checksec --file=GccUnixR/uasm

RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH      Symbols         FORTIFY Fortified       Fortifiable     FILE
Full RELRO      Canary found      NX enabled    PIE enabled     No RPATH   No RUNPATH   No Symbols      Yes     11              20              GccUnixR/uasm

There are only 8 warnings left.
warnings.txt

@teoberi
Copy link

teoberi commented Oct 9, 2024

UASM/assemble.c

Line 1041 in 540d215

/* UASM 2.55 , altname is set to 1 when the type is undefined */

UASM/assemble.c

Line 1042 in 540d215

if ( curr->sym.altname > 1 )

Produces the first warning from those exposed in the previous post (warnings.txt).
Could it be used?
if ( (intptr_t)curr->sym.altname > 1 )

https://stackoverflow.com/questions/22624737/casting-a-pointer-to-an-int
https://stackoverflow.com/questions/6326338/why-when-to-use-intptr-t-for-type-casting-in-c

If it is a viable solution can it be used to resolve all remaining warnings?

@darealshinji
Copy link
Contributor Author

Some of the Linux test files in regress/src/linux64/ don't work correctly after assembling and linking.
But even before these fixes Lin64_3.asm prints "Hello, world!J" instead of "Hello, world!Jane Doe".

Maybe you just have to stick to an old version of GCC to compile uasm?

@teoberi
Copy link

teoberi commented Oct 9, 2024

It might be a good start for fixing the (lots of) compilation warnings that have been there since I used it!

@teoberi
Copy link

teoberi commented Oct 14, 2024

I have built 7-Zip 24.08 using GCC version 14.2.0 + UASM with all the patches without any errors.

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/LzmaDecOpt.asm
LzmaDecOpt.asm: 1339 lines, 3 passes, 6623 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/7zCrcOpt.asm
7zCrcOpt.asm: 258 lines, 3 passes, 2173 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/AesOpt.asm
"++ VAES 256"
"x86-64"
"ABI : LINUX"
AesOpt.asm: 742 lines, 2 passes, 4322 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/Sha1Opt.asm
Sha1Opt.asm: 263 lines, 2 passes, 2357 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/Sha256Opt.asm
Sha256Opt.asm: 275 lines, 2 passes, 2256 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/XzCrc64Opt.asm
XzCrc64Opt.asm: 523 lines, 3 passes, 1920 ms, 0 warnings, 0 errors

uasm -nologo -elf64 -DABI_LINUX -Fob/g_x64/ ../../../../Asm/x86/LzFindOpt.asm
LzFindOpt.asm: 540 lines, 3 passes, 2356 ms, 0 warnings, 0 errors

@darealshinji
Copy link
Contributor Author

I've tested it with the 7zip sources and both the patched and unpatched UASM produce exactly the same output.

@teoberi
Copy link

teoberi commented Oct 18, 2024

Correct! But the unpatched version does not compile in GCC 14.2.0!

@bwrsandman
Copy link

Still some errors when making with make DEBUG=1 -f Makefile-Linux.mak

parser.c:3758:103: error: ‘instr’ undeclared (first use in this function); did you mean ‘int’?
 3758 |                                         DebugMsg(("ParseLine(%s,%u): avx not enough operands (%u)\n", instr, CurrOpnd, opndx[OPND2].kind, j));

@darealshinji
Copy link
Contributor Author

Debug build should work now.

@darealshinji
Copy link
Contributor Author

Got rid of the const qualifier issues. There are still many -Wmisleading-indentation warnings with -Wall, because the code uses a mix of tab and space indentation and at the same time often misses curly braces. But I'm not trying to fix these, someone with more knowledge about the code has to do that.

@darealshinji darealshinji changed the title Silencing GCC compiler warnings Linux: build on newer GCC and Clang releases, silence compiler warnings Dec 13, 2024
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