MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by adeyblue »

Anybody know how/if you can get MinGW to add one of these things to the exe header?

In Visual Studio you do this

Code: Select all

extern IMAGE_LOAD_CONFIG_DIRECTORY _load_config_used = {
    0x24, // fill these with whatever
    0x12345678
};
And the VS linker knows to look for _load_config_used and does the bizzo, filling in the IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG exe optional header directory with a pointer to it. MinGW doesn't know the magic name. so if its possible, there's some other method to get it to do that, but if its possible I don't have the google-fu to find it. Bonus points if you also know how to get GAS to do the same.

As for what this has to do with FB, as part of fixing On Error Goto I'm adding some SEH stuff to the compiler, and this struct is how you enable SafeSEH.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by dodicat »

The only thing I can think of is the work by member caseih
viewtopic.php?p=286067&hilit=%2A_binary_#p286067

Example, getting an array into an executable.
file1

Code: Select all


type fstring as zstring * 40

sub save(file as string,u() as fstring)
    var h=freefile
    open file for binary access write as #h
    put #h, ,u()
    close #h
end sub


dim as fstring g(1 to 4)={"THIS ARRAY WILL BE INCLUDED",_
                          !"\x59\x6f\x75\x20\x64\x6f\x6e\x27\x74\x20\x67\x65\x74\x20\x6d\x65\x2c",_
                          !"\x49\x27\x6d\x20\x70\x61\x72\x74\x20\x6f\x66\x20\x74\x68\x65\x20\x75\x6e\x69\x6f\x6e\x2e",_
                          !"\x49\x20\x61\x6d\x20\x61\x20\x75\x6e\x69\x6f\x6e\x20\x6d\x61\x6e\x2e"}

save("addtext.txt",g())

#macro ldcmd
ld.exe -r -b  binary -o mytext.o addtext.txt ''<use this

(to get the object file mytext.o)
#endmacro 
use the instruction (in the macro as a reminder)
ld.exe -r -b binary -o mytext.o addtext.txt
Then file2

Code: Select all

#include "crt.bi"
#cmdline "mytext.o"

extern _start alias "_binary_addtext_txt_start" as byte
extern _end alias "_binary_addtext_txt_end" as byte

dim as long length=@_end - @_start

type fstring as zstring * 40

dim as long L=(@_end-@_start)\sizeof(fstring)

dim  as fstring s(1 to L)

memcpy(@s(1),@_start,length)

for n as long=1 to ubound(s)
print s(n)
next
sleep
 
will have the array in file2.exe
This is for 64 bits, and probably of no use to you whatsoever.
But I Will agree onerror needs attention.
If you want to test just use a dedicated folder for the files.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by TeeEmCee »

I don't know much about SEH but it looks to me (e.g. trying to interpret this) that this is something the MSVC and LLVM linkers usually add automatically if appropriate. LLVM does look for _load_config_used. No idea about GNU ld. Did you check that it's actually missing from compiled .exes?
Also, GCC apparently does not support SEH on x86, only on x86_64, where SEH works "quite differently". From that MSVC page:
/SAFESEH is only valid when linking for x86 targets. /SAFESEH isn't supported for platforms that already have the exception handlers noted. For example, on x64 and ARM, all exception handlers are noted in the PDATA.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by dodicat »

In fbide using the run command
cmd /c "<$file>" <$param> & pause
keeps the console open, thus using
#cmdline "-exx"
behaves exactly as on error goto, that is the runtime error number is shown to the user.
So on error goto is redundant with this ide and the given run command.
The #cmdline options allow compiler switches via code.
Would it be impossible to give the executable a command to keep the console open via code?
Would it be easier than injecting stuff into an executable via MinGW?
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by adeyblue »

TeeEmCee wrote: May 10, 2022 8:08 Did you check that it's actually missing from compiled .exes?
Yep
https://prnt.sc/HD6jSu_yx-pv
Also, GCC apparently does not support SEH on x86, only on x86_64, where SEH works "quite differently". From that MSVC page:
GCC doesn't support x86 SEH in the same sense that FB doesn't support MP3 output. There's no way to do it out of the box, but if you write the appropriate code yourself, you can certainly make it happen.

Anyway, I checked the source code of LD/BFD and there's no references to _load_config_used or any direct reference to IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG (or PE_LOAD_CONFIG_TABLE) so I guess there's no automatic doing of that. LD does have a plugin interface but it's very much built for and around what GCC Link Time Optimization requires, so it's pretty useless for any purpose that requires final symbol addresses/offsets in the image.

The only way then seems to be to have LD generate a map file, parse that, and then edit the exe/dll header afterwards.
dodicat wrote: May 10, 2022 9:07 Would it be easier than injecting stuff into an executable via MinGW?
The problem I'm fixing are the ones that happen when the thing that causes the Error, and the On Error Goto statement are in different subs/functions. If you Print local variables after the Goto has happened, they'll either crash your app or have different values to what they did/should do.

I don't have to use SEH to do it, but it enables FB to participate in and interoperate with other languages that support it.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: MinGW & IMAGE_LOAD_CONFIG_DIRECTORY

Post by dodicat »

Leaving the console open after an error with option -exx finds all the 17 instances of run time errors whether inside subs or not.
So it a failsafe way of doing the job for these 17 at least, hopefully.
It doesn't of course do leaks.
However on error is unpredictable, it does the job here, when it shouldn't really.

Code: Select all

#cmdline "-exx"

on error goto fin

declare function one as any ptr
function two as any ptr
      return one
end function

type udt extends object
       declare function f() as any ptr
end type
      
function udt.f as any ptr
      dim as function as any ptr z= @two
     return z()
end function
      
dim as udt x
print x.f
sleep
end


fin:
 if Err() then
    Dim As Integer Errnum = Err()
    print "Error ";Errnum;"  =  ";
    select case errnum
case 0 :print "No error" 
case 1 :print  "Illegal function call" 
case 2 :print  "File not found signal" 
case 3 :print  "File I/O error" 
case 4 :print  "Out of memory" 
case 5 :print  "Illegal resume" 
case 6 :print "Out of bounds array access" 
case 7 :print  "Null Pointer Access" 
case 8 :print  "No privileges" 
case 9 :print  "interrupted signal" 
case 10 :print  "illegal instruction signal" 
case 11 :print  "floating point error signal" 
case 12 :print  "segmentation violation signal" 
case 13 :print  "Termination request signal" 
case 14 :print  "abnormal termination signal" 
case 15 :print  "quit request signal" 
case 16 :print  "return without gosub" 
case 17 :print  "end of file" 
end select
    print "Error function ";*erfn;"  "+__function__
    print "Error line ";Erl
    sleep
    end errnum
end if


function one as any ptr
      dim as long ptr p
      print *p
      return p
end function

 
Error 7 = Null Pointer Access
Error function ONE __FB_MAINPROC__
Error line 59
Post Reply