bug-binutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug ld/30145] No way to specify PE IMAGE_SCN_* characteristics


From: pali at kernel dot org
Subject: [Bug ld/30145] No way to specify PE IMAGE_SCN_* characteristics
Date: Sat, 15 Apr 2023 10:40:19 +0000

https://sourceware.org/bugzilla/show_bug.cgi?id=30145

--- Comment #3 from Pali Rohár <pali at kernel dot org> ---
Ok, I have tested objcopy --set-section-flags on PE executable. Seems that it
is doing something, but only for some of the flags and I really do not
understand what it is doing. For me it looks like that objcopy
--set-section-flags sets random IMAGE_SCN_* characteristics...

Here is my test case and output from readpe after setting every flag via
objcopy --set-section-flags on simple PE executable with custom section
my_sect:

$ cat test-section-flags.c 
__attribute__((section("my_sect"))) int val = 10;
int my_entry(void) { return val; }


$ x86_64-w64-mingw32-gcc -Wall -Wextra -nostartfiles -nodefaultlibs -nostdlib
-e my_entry test-section-flags.c -o test-section-flags.exe


$ readpe test-section-flags.exe 2>/dev/null | grep -A 11 my_sect
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000040
        Characteristic Names
                                             IMAGE_SCN_CNT_INITIALIZED_DATA
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section


$ for flag in alloc load noload readonly debug code data rom exclude share
contents merge strings; do echo "Flags: $flag"; x86_64-w64-mingw32-objcopy
--set-section-flags my_sect=$flag test-section-flags.exe
test-section-flags-new.exe && readpe test-section-flags-new.exe 2>/dev/null |
grep -A 12 my_sect; echo; done
Flags: alloc
        Name:                            my_sect
        Virtual Size:                    0x200 (512 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0 (0 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000080
        Characteristic Names
                                             IMAGE_SCN_CNT_UNINITIALIZED_DATA
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata

Flags: load
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)

Flags: noload
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc2000000
        Characteristic Names
                                             IMAGE_SCN_MEM_DISCARDABLE
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata

Flags: readonly
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0x40000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)
        Virtual Address:                 0x3000

Flags: debug
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc2000040
        Characteristic Names
                                             IMAGE_SCN_CNT_INITIALIZED_DATA
                                             IMAGE_SCN_MEM_DISCARDABLE
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section

Flags: code
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xe0000020
        Characteristic Names
                                             IMAGE_SCN_CNT_CODE
                                             IMAGE_SCN_MEM_EXECUTE
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section

Flags: data
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000040
        Characteristic Names
                                             IMAGE_SCN_CNT_INITIALIZED_DATA
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata

Flags: rom
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)

Flags: exclude
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc2000000
        Characteristic Names
                                             IMAGE_SCN_MEM_DISCARDABLE
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata

Flags: share
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xd0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_SHARED
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata

Flags: contents
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)

Flags: merge
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)

Flags: strings
        Name:                            my_sect
        Virtual Size:                    0x4 (4 bytes)
        Virtual Address:                 0x2000
        Size Of Raw Data:                0x200 (512 bytes)
        Pointer To Raw Data:             0x600
        Number Of Relocations:           0
        Characteristics:                 0xc0000000
        Characteristic Names
                                             IMAGE_SCN_MEM_READ
                                             IMAGE_SCN_MEM_WRITE
    Section
        Name:                            .rdata
        Virtual Size:                    0x20 (32 bytes)



This --set-section-flags objcopy option should be properly documented for every
flag, what it is doing with PE executable, because from above output I'm really
not sure.

Now I remember that in past I was trying to set IMAGE_SCN_MEM_DISCARDABLE via
objcopy 2.31.1 and none of flags was able to do it (neither noload). But with
objcopy 2.39 it is possible via noload flag.

So... this --set-section-flags option looks like a good starting point for
setting section flags. Just it needs documentation and fixing/extending it for
other PE IMAGE_SCN_* flags.

And important is also ability to set or clear just one particular flag, because
currently --set-section-flags can just replace all existing flags by a new
flags specified in the command line option.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]