1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Silence gcc's -Wstrict-aliasing=1 warnings.

This commit is contained in:
Markus F.X.J. Oberhumer 2008-12-29 23:15:56 +01:00
parent 901c6b34fa
commit 063bd58802
6 changed files with 63 additions and 42 deletions

View File

@ -844,13 +844,16 @@ Elf32_Shdr const *PackLinuxElf32::elf_find_section_type(
bool PackLinuxElf32::canPack() bool PackLinuxElf32::canPack()
{ {
unsigned char buf[sizeof(Elf32_Ehdr) + 14*sizeof(Elf32_Phdr)]; union {
COMPILE_TIME_ASSERT(sizeof(buf) <= 512); unsigned char buf[sizeof(Elf32_Ehdr) + 14*sizeof(Elf32_Phdr)];
struct { Elf32_Ehdr ehdr; Elf32_Phdr phdr; } e;
} u;
COMPILE_TIME_ASSERT(sizeof(u.buf) <= 512);
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(buf, sizeof(buf)); fi->readx(u.buf, sizeof(u.buf));
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
Elf32_Ehdr const *const ehdr = (Elf32_Ehdr const *)buf; Elf32_Ehdr const *const ehdr = &u.e.ehdr;
// now check the ELF header // now check the ELF header
if (checkEhdr(ehdr) != 0) if (checkEhdr(ehdr) != 0)
@ -868,10 +871,10 @@ bool PackLinuxElf32::canPack()
return false; return false;
} }
unsigned osabi0 = buf[Elf32_Ehdr::EI_OSABI]; unsigned osabi0 = u.buf[Elf32_Ehdr::EI_OSABI];
// The first PT_LOAD32 must cover the beginning of the file (0==p_offset). // The first PT_LOAD32 must cover the beginning of the file (0==p_offset).
unsigned const e_phnum = get_te16(&ehdr->e_phnum); unsigned const e_phnum = get_te16(&ehdr->e_phnum);
Elf32_Phdr const *phdr = (Elf32_Phdr const *)(buf + e_phoff); Elf32_Phdr const *phdr = (Elf32_Phdr const *)(u.buf + e_phoff);
for (unsigned j=0; j < e_phnum; ++phdr, ++j) { for (unsigned j=0; j < e_phnum; ++phdr, ++j) {
if (j >= 14) if (j >= 14)
return false; return false;
@ -1002,12 +1005,15 @@ found:
bool bool
PackLinuxElf64amd::canPack() PackLinuxElf64amd::canPack()
{ {
unsigned char buf[sizeof(Elf64_Ehdr) + 14*sizeof(Elf64_Phdr)]; union {
COMPILE_TIME_ASSERT(sizeof(buf) <= 1024); unsigned char buf[sizeof(Elf64_Ehdr) + 14*sizeof(Elf64_Phdr)];
struct { Elf64_Ehdr ehdr; Elf64_Phdr phdr; } e;
} u;
COMPILE_TIME_ASSERT(sizeof(u) <= 1024);
fi->readx(buf, sizeof(buf)); fi->readx(u.buf, sizeof(u.buf));
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
Elf64_Ehdr const *const ehdr = (Elf64_Ehdr const *)buf; Elf64_Ehdr const *const ehdr = &u.e.ehdr;
// now check the ELF header // now check the ELF header
if (checkEhdr(ehdr) != 0) if (checkEhdr(ehdr) != 0)
@ -1026,7 +1032,7 @@ PackLinuxElf64amd::canPack()
// The first PT_LOAD64 must cover the beginning of the file (0==p_offset). // The first PT_LOAD64 must cover the beginning of the file (0==p_offset).
unsigned const e_phnum = get_te16(&ehdr->e_phnum); unsigned const e_phnum = get_te16(&ehdr->e_phnum);
Elf64_Phdr const *phdr = (Elf64_Phdr const *)(buf + (unsigned) e_phoff); Elf64_Phdr const *phdr = (Elf64_Phdr const *)(u.buf + (unsigned) e_phoff);
for (unsigned j=0; j < e_phnum; ++phdr, ++j) { for (unsigned j=0; j < e_phnum; ++phdr, ++j) {
if (j >= 14) if (j >= 14)
return false; return false;
@ -1104,8 +1110,8 @@ PackLinuxElf32::generateElfHdr(
unsigned const brka unsigned const brka
) )
{ {
cprElfHdr2 *const h2 = (cprElfHdr2 *)&elfout; cprElfHdr2 *const h2 = (cprElfHdr2 *)(void *)&elfout;
cprElfHdr3 *const h3 = (cprElfHdr3 *)&elfout; cprElfHdr3 *const h3 = (cprElfHdr3 *)(void *)&elfout;
memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK
h3->ehdr.e_ident[Elf32_Ehdr::EI_OSABI] = ei_osabi; h3->ehdr.e_ident[Elf32_Ehdr::EI_OSABI] = ei_osabi;
if (Elf32_Ehdr::EM_MIPS==e_machine) { // MIPS R3000 FIXME if (Elf32_Ehdr::EM_MIPS==e_machine) { // MIPS R3000 FIXME
@ -1232,8 +1238,8 @@ PackLinuxElf64::generateElfHdr(
unsigned const brka unsigned const brka
) )
{ {
cprElfHdr2 *const h2 = (cprElfHdr2 *)&elfout; cprElfHdr2 *const h2 = (cprElfHdr2 *)(void *)&elfout;
cprElfHdr3 *const h3 = (cprElfHdr3 *)&elfout; cprElfHdr3 *const h3 = (cprElfHdr3 *)(void *)&elfout;
memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK
h3->ehdr.e_ident[Elf32_Ehdr::EI_OSABI] = ei_osabi; h3->ehdr.e_ident[Elf32_Ehdr::EI_OSABI] = ei_osabi;
@ -1888,14 +1894,17 @@ void PackLinuxElf64::pack4(OutputFile *fo, Filter &ft)
void PackLinuxElf32::unpack(OutputFile *fo) void PackLinuxElf32::unpack(OutputFile *fo)
{ {
#define MAX_ELF_HDR 512 #define MAX_ELF_HDR 512
char bufehdr[MAX_ELF_HDR]; union {
Elf32_Ehdr *const ehdr = (Elf32_Ehdr *)bufehdr; char buf[MAX_ELF_HDR];
Elf32_Phdr const *phdr = (Elf32_Phdr *)(1+ehdr); struct { Elf32_Ehdr ehdr; Elf32_Phdr phdr; } e;
} u;
Elf32_Ehdr *const ehdr = &u.e.ehdr;
Elf32_Phdr const *phdr = &u.e.phdr;
unsigned szb_info = sizeof(b_info); unsigned szb_info = sizeof(b_info);
{ {
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(bufehdr, MAX_ELF_HDR); fi->readx(u.buf, MAX_ELF_HDR);
unsigned const e_entry = get_te32(&ehdr->e_entry); unsigned const e_entry = get_te32(&ehdr->e_entry);
if (e_entry < 0x401180 if (e_entry < 0x401180
&& ehdr->e_machine==Elf32_Ehdr::EM_386) { /* old style, 8-byte b_info */ && ehdr->e_machine==Elf32_Ehdr::EM_386) { /* old style, 8-byte b_info */
@ -1949,7 +1958,7 @@ void PackLinuxElf32::unpack(OutputFile *fo)
} }
} }
phdr = (Elf32_Phdr *)(1+ehdr); phdr = &u.e.phdr;
for (unsigned j = 0; j < phnum; ++j) { for (unsigned j = 0; j < phnum; ++j) {
unsigned const size = find_LOAD_gap(phdr, j, phnum); unsigned const size = find_LOAD_gap(phdr, j, phnum);
if (size) { if (size) {
@ -1993,14 +2002,17 @@ void PackLinuxElf32::unpack(OutputFile *fo)
void PackLinuxElf64::unpack(OutputFile *fo) void PackLinuxElf64::unpack(OutputFile *fo)
{ {
#define MAX_ELF_HDR 1024 #define MAX_ELF_HDR 1024
char bufehdr[MAX_ELF_HDR]; union {
Elf64_Ehdr *const ehdr = (Elf64_Ehdr *)bufehdr; char buf[MAX_ELF_HDR];
Elf64_Phdr const *phdr = (Elf64_Phdr *)(1+ehdr); struct { Elf64_Ehdr ehdr; Elf64_Phdr phdr; } e;
} u;
Elf64_Ehdr *const ehdr = &u.e.ehdr;
Elf64_Phdr const *phdr = &u.e.phdr;
unsigned szb_info = sizeof(b_info); unsigned szb_info = sizeof(b_info);
{ {
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(bufehdr, MAX_ELF_HDR); fi->readx(u.buf, MAX_ELF_HDR);
acc_uint64l_t const e_entry = get_te64(&ehdr->e_entry); acc_uint64l_t const e_entry = get_te64(&ehdr->e_entry);
if (e_entry < 0x401180 if (e_entry < 0x401180
&& ehdr->e_machine==Elf64_Ehdr::EM_386) { /* old style, 8-byte b_info */ && ehdr->e_machine==Elf64_Ehdr::EM_386) { /* old style, 8-byte b_info */
@ -2054,7 +2066,7 @@ void PackLinuxElf64::unpack(OutputFile *fo)
} }
} }
phdr = (Elf64_Phdr *)(1+ehdr); phdr = &u.e.phdr;
for (unsigned j = 0; j < phnum; ++j) { for (unsigned j = 0; j < phnum; ++j) {
unsigned const size = find_LOAD_gap(phdr, j, phnum); unsigned const size = find_LOAD_gap(phdr, j, phnum);
if (size) { if (size) {
@ -2354,14 +2366,17 @@ Elf32_Sym const *PackLinuxElf32::elf_lookup(char const *name) const
void PackLinuxElf32x86::unpack(OutputFile *fo) void PackLinuxElf32x86::unpack(OutputFile *fo)
{ {
#define MAX_ELF_HDR 512 #define MAX_ELF_HDR 512
char bufehdr[MAX_ELF_HDR]; union {
Elf32_Ehdr *const ehdr = (Elf32_Ehdr *)bufehdr; char buf[MAX_ELF_HDR];
Elf32_Phdr const *phdr = (Elf32_Phdr *)(1+ehdr); struct { Elf32_Ehdr ehdr; Elf32_Phdr phdr; } e;
} u;
Elf32_Ehdr *const ehdr = &u.e.ehdr;
Elf32_Phdr const *phdr = &u.e.phdr;
unsigned szb_info = sizeof(b_info); unsigned szb_info = sizeof(b_info);
{ {
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(bufehdr, MAX_ELF_HDR); fi->readx(u.buf, MAX_ELF_HDR);
unsigned const e_entry = get_te32(&ehdr->e_entry); unsigned const e_entry = get_te32(&ehdr->e_entry);
unsigned const e_type = get_te16(&ehdr->e_type); unsigned const e_type = get_te16(&ehdr->e_type);
if (e_entry < 0x401180 && Elf32_Ehdr::ET_EXEC==e_type) { if (e_entry < 0x401180 && Elf32_Ehdr::ET_EXEC==e_type) {
@ -2415,7 +2430,7 @@ void PackLinuxElf32x86::unpack(OutputFile *fo)
} }
} }
phdr = (Elf32_Phdr *)(1+ehdr); phdr = &u.e.phdr;
for (unsigned j = 0; j < phnum; ++j) { for (unsigned j = 0; j < phnum; ++j) {
unsigned const size = find_LOAD_gap(phdr, j, phnum); unsigned const size = find_LOAD_gap(phdr, j, phnum);
if (size) { if (size) {

View File

@ -132,8 +132,8 @@ PackLinuxI386::generateElfHdr(
unsigned const brka unsigned const brka
) )
{ {
cprElfHdr2 *const h2 = (cprElfHdr2 *)&elfout; cprElfHdr2 *const h2 = (cprElfHdr2 *)(void *)&elfout;
cprElfHdr3 *const h3 = (cprElfHdr3 *)&elfout; cprElfHdr3 *const h3 = (cprElfHdr3 *)(void *)&elfout;
memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK memcpy(h3, proto, sizeof(*h3)); // reads beyond, but OK
assert(h2->ehdr.e_phoff == sizeof(Elf32_Ehdr)); assert(h2->ehdr.e_phoff == sizeof(Elf32_Ehdr));
@ -528,11 +528,14 @@ bool PackLinuxI386::canPack()
default: default:
unsigned const e_phnum = get_te16(&ehdr.e_phnum); unsigned const e_phnum = get_te16(&ehdr.e_phnum);
if (e_phnum<=(512/sizeof(Elf32_Phdr))) { if (e_phnum<=(512/sizeof(Elf32_Phdr))) {
char buf2[512]; union {
char buf2[512];
Elf32_Phdr phdr;
} u;
fi->seek(get_te32(&ehdr.e_phoff), SEEK_SET); fi->seek(get_te32(&ehdr.e_phoff), SEEK_SET);
fi->readx(buf2, sizeof(buf2)); fi->readx(u.buf2, sizeof(u.buf2));
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
Elf32_Phdr const *phdr = (Elf32_Phdr const *)buf2; Elf32_Phdr const *phdr = &u.phdr;
for (unsigned j=0; j < e_phnum; ++phdr, ++j) { for (unsigned j=0; j < e_phnum; ++phdr, ++j) {
if (phdr->PT_NOTE == get_te32(&phdr->p_type)) { if (phdr->PT_NOTE == get_te32(&phdr->p_type)) {
unsigned const offset = get_te32(&phdr->p_offset); unsigned const offset = get_te32(&phdr->p_offset);

View File

@ -197,14 +197,17 @@ void PackLinuxElf32x86interp::pack3(OutputFile *fo, Filter &/*ft*/)
void PackLinuxElf32x86interp::unpack(OutputFile *fo) void PackLinuxElf32x86interp::unpack(OutputFile *fo)
{ {
#define MAX_INTERP_HDR 512 #define MAX_INTERP_HDR 512
char bufehdr[MAX_INTERP_HDR]; union {
Elf32_Ehdr *const ehdr = (Elf32_Ehdr *)bufehdr; char buf[MAX_INTERP_HDR];
Elf32_Phdr const *phdr = (Elf32_Phdr *)(1+ehdr); struct { Elf32_Ehdr ehdr; Elf32_Phdr phdr; } e;
} u;
Elf32_Ehdr *const ehdr = &u.e.ehdr;
Elf32_Phdr const *phdr = &u.e.phdr;
unsigned szb_info = sizeof(b_info); unsigned szb_info = sizeof(b_info);
{ {
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(bufehdr, MAX_INTERP_HDR); fi->readx(u.buf, MAX_INTERP_HDR);
unsigned const e_entry = get_te32(&ehdr->e_entry); unsigned const e_entry = get_te32(&ehdr->e_entry);
if (e_entry < 0x401180) { /* old style, 8-byte b_info */ if (e_entry < 0x401180) { /* old style, 8-byte b_info */
szb_info = 2*sizeof(unsigned); szb_info = 2*sizeof(unsigned);

View File

@ -643,7 +643,7 @@ void PackMachBase<T>::unpack(OutputFile *fo)
unsigned total_out = 0; unsigned total_out = 0;
unsigned c_adler = upx_adler32(NULL, 0); unsigned c_adler = upx_adler32(NULL, 0);
unsigned u_adler = upx_adler32(NULL, 0); unsigned u_adler = upx_adler32(NULL, 0);
Mach_segment_command const *sc = (Mach_segment_command const *)(1+ mhdr); Mach_segment_command const *sc = (Mach_segment_command const *)(void *)(1+ mhdr);
unsigned k; unsigned k;
fi->seek(- (off_t)(sizeof(bhdr) + ph.c_len), SEEK_CUR); fi->seek(- (off_t)(sizeof(bhdr) + ph.c_len), SEEK_CUR);

View File

@ -433,7 +433,7 @@ bool PackPs1::findBssSection()
reg = REG1(op); reg = REG1(op);
for (; i >= 0; i--) for (; i >= 0; i--)
{ {
bss_nfo *p = (bss_nfo *)&p1[i]; bss_nfo *p = (bss_nfo *)(void *)&p1[i];
unsigned short op1 = p->op1, op2 = p->op2; unsigned short op1 = p->op1, op2 = p->op2;
// check for la (x),bss_start // check for la (x),bss_start

View File

@ -343,7 +343,7 @@ void PeFile::Reloc::finish(upx_byte *&p,unsigned &siz)
qsort(start + 1024,counts[0],4,le32_compare); qsort(start + 1024,counts[0],4,le32_compare);
rel = (reloc*) start; rel = (reloc*) start;
rel1 = (LE16*) rel; rel1 = (LE16*) start;
for (unsigned ic = 0; ic < counts[0]; ic++) for (unsigned ic = 0; ic < counts[0]; ic++)
{ {
unsigned pos = get_le32(start + 1024 + 4 * ic); unsigned pos = get_le32(start + 1024 + 4 * ic);