Reputation: 5480
I am trying to build https://github.com/TheCodeArtist/elf-parser under MINGW64 (MSYS2) bash
shell in Windows 10.
I have made some patches so far, which are saved as /tmp/elf-parser-mingw.patch
:
diff --git a/elf-parser.c b/elf-parser.c
index a6b180c..1c076f1 100644
--- a/elf-parser.c
+++ b/elf-parser.c
@@ -109,9 +109,9 @@ void print_elf_header64(Elf64_Ehdr elf_header)
printf("OpenBSD\n");
break;
- case ELFOSABI_ARM_AEABI:
- printf("ARM EABI\n");
- break;
+// case ELFOSABI_ARM_AEABI:
+// printf("ARM EABI\n");
+// break;
case ELFOSABI_ARM:
printf("ARM\n");
@@ -391,7 +391,7 @@ void save_text_section64(int32_t fd, Elf64_Ehdr eh, Elf64_Shdr sh_table[])
assert(read(fd, buf, sh_table[i].sh_size)==sh_table[i].sh_size);
fd2 = open(pwd, O_RDWR|O_SYNC|O_CREAT, 0644);
write(fd2, buf, sh_table[i].sh_size);
- fsync(fd2);
+ //fsync(fd2);
EXIT:
close(fd2);
@@ -510,9 +510,9 @@ void print_elf_header(Elf32_Ehdr elf_header)
printf("OpenBSD\n");
break;
- case ELFOSABI_ARM_AEABI:
- printf("ARM EABI\n");
- break;
+// case ELFOSABI_ARM_AEABI:
+// printf("ARM EABI\n");
+// break;
case ELFOSABI_ARM:
printf("ARM\n");
@@ -788,7 +788,7 @@ void save_text_section(int32_t fd, Elf32_Ehdr eh, Elf32_Shdr sh_table[])
assert(read(fd, buf, sh_table[i].sh_size)==sh_table[i].sh_size);
fd2 = open(pwd, O_RDWR|O_SYNC|O_CREAT, 0644);
write(fd2, buf, sh_table[i].sh_size);
- fsync(fd2);
+ //fsync(fd2);
EXIT:
close(fd2);
diff --git a/elf-parser.h b/elf-parser.h
index 2d8ce33..681f008 100644
--- a/elf-parser.h
+++ b/elf-parser.h
@@ -1,14 +1,22 @@
+#include <elf.h>
+#undef errno
+#undef assert
+#undef _INC_ERRNO
+#undef _CRT_ERRNO_DEFINED
+
+// from MSYS2 /usr/include/sys/_default_fcntl.h, since MINGW64 fcntl.h does not define it
+#define _FSYNC 0x2000 /* do all writes synchronously */
+#define O_SYNC _FSYNC
+
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
-#include <errno.h>
+//#include <errno.h>
#include <stdbool.h>
-#include <elf.h>
-
#define DEBUG 1
#define debug(...) \
Here is how the warning can be reproduced - first, get the code and apply patch:
cd /tmp/
git clone https://github.com/TheCodeArtist/elf-parser.git
cd elf-parser
git apply /tmp/elf-parser-mingw.patch
... and then build elf-parser.c
with:
gcc -c -o elf-parser.o elf-parser.c -I. -O3 -DNDEBUG -Wall -std=c99 -idirafter D:/msys64/mingw64/arm-none-eabi/include
The file compiles, but I get many warnings - and among them, I get:
elf-parser.c:376:21: warning: '<unknown>' may be used uninitialized [-Wmaybe-uninitialized]
376 | if(!strcmp(".text", (sh_str + sh_table[i].sh_name))) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from C:/msys64/mingw64/include/io.h:10,
from C:/msys64/mingw64/include/fcntl.h:8,
from ./elf-parser.h:13,
from elf-parser.c:1:
C:/msys64/mingw64/include/string.h:63:15: note: by argument 2 of type 'const char *' to 'strcmp' declared here
63 | int __cdecl strcmp(const char *_Str1,const char *_Str2);
| ^~~~~~
The line 376 that triggers the warning is here: https://github.com/TheCodeArtist/elf-parser/blob/master/elf-parser.c#L376
Now, this puzzles me a lot; first of all, what is this '' that "may be used uninitialized"?
By the note "note: by argument 2 of type 'const char *' to 'strcmp' declared here", I assume the '' refers to the second argument of strcmp
, in this case sh_table[i].sh_name
. As far as I can see in https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-94076/index.html, .sh_name
should be of type Elf32_Word - in any case, a numeric type; and sh_str
is defined as a char*
(char pointer); so basically we have a char pointer to which an offset is added; but even if a totally random address is computed as a result of this, the worst that can happen is that the strcmp will fail - so I'm having a hard time seeing the "uninitialized" problem here?! Could anyone explain?
And finally - what needs to be done in this code, to get rid of the "'' may be used uninitialized" warning?
Upvotes: -1
Views: 60