SDL2/SDL_Image loading objcopy files

0

As stated on the topic, i'm using objcopy to load SDL_Image images with MinGW over Eclipse Helios for windows. I'm using the command: objcopy --input-target binary --output-target pei-i386 --binary-architecture i386 Lilothyn.jpg Lilothyn.o That results in the file:

Lilothyn.o:     file format pei-i386
Lilothyn.o
architecture: i386, flags 0x00000030:
HAS_SYMS, HAS_LOCALS
start address 0x00000000

Characteristics 0x305
        relocations stripped
        line numbers stripped
        32 bit words
        debugging information removed

Time/Date               Thu Jan 01 00:00:00 1970
Magic                   0000
MajorLinkerVersion      0
MinorLinkerVersion      0
SizeOfCode              00000000
SizeOfInitializedData   00000000
SizeOfUninitializedData 00000000
AddressOfEntryPoint     00000000
BaseOfCode              00000000
BaseOfData              00000000
ImageBase               00000000
SectionAlignment        00000000
FileAlignment           00000000
MajorOSystemVersion     0
MinorOSystemVersion     0
MajorImageVersion       0
MinorImageVersion       0
MajorSubsystemVersion   0
MinorSubsystemVersion   0
Win32Version            00000000
SizeOfImage             00000000
SizeOfHeaders           00000000
CheckSum                00000000
Subsystem               00000000        (unspecified)
DllCharacteristics      00000000
SizeOfStackReserve      00000000
SizeOfStackCommit       00000000
SizeOfHeapReserve       00000000
SizeOfHeapCommit        00000000
LoaderFlags             00000000
NumberOfRvaAndSizes     00000000

The Data Directory
Entry 0 00000000 00000000 Export Directory [.edata (or where ever we found it)]
Entry 1 00000000 00000000 Import Directory [parts of .idata]
Entry 2 00000000 00000000 Resource Directory [.rsrc]
Entry 3 00000000 00000000 Exception Directory [.pdata]
Entry 4 00000000 00000000 Security Directory
Entry 5 00000000 00000000 Base Relocation Directory [.reloc]
Entry 6 00000000 00000000 Debug Directory
Entry 7 00000000 00000000 Description Directory
Entry 8 00000000 00000000 Special Directory
Entry 9 00000000 00000000 Thread Storage Directory [.tls]
Entry a 00000000 00000000 Load Configuration Directory
Entry b 00000000 00000000 Bound Import Directory
Entry c 00000000 00000000 Import Address Table Directory
Entry d 00000000 00000000 Delay Import Directory
Entry e 00000000 00000000 CLR Runtime Header
Entry f 00000000 00000000 Reserved

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         000024fb  00000000  00000000  000000c0  2**0
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
[  0](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _binary_Lilothyn_jpg_start
[  1](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x000024fb _binary_Lilothyn_jpg_end
[  2](sec -1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x000024fb _binary_Lilothyn_jpg_size

... and the other:

Tommy.o:     file format pei-i386
Tommy.o
architecture: i386, flags 0x00000030:
HAS_SYMS, HAS_LOCALS
start address 0x00000000

Characteristics 0x305
        relocations stripped
        line numbers stripped
        32 bit words
        debugging information removed

Time/Date               Thu Jan 01 00:00:00 1970
Magic                   0000
MajorLinkerVersion      0
MinorLinkerVersion      0
SizeOfCode              00000000
SizeOfInitializedData   00000000
SizeOfUninitializedData 00000000
AddressOfEntryPoint     00000000
BaseOfCode              00000000
BaseOfData              00000000
ImageBase               00000000
SectionAlignment        00000000
FileAlignment           00000000
MajorOSystemVersion     0
MinorOSystemVersion     0
MajorImageVersion       0
MinorImageVersion       0
MajorSubsystemVersion   0
MinorSubsystemVersion   0
Win32Version            00000000
SizeOfImage             00000000
SizeOfHeaders           00000000
CheckSum                00000000
Subsystem               00000000        (unspecified)
DllCharacteristics      00000000
SizeOfStackReserve      00000000
SizeOfStackCommit       00000000
SizeOfHeapReserve       00000000
SizeOfHeapCommit        00000000
LoaderFlags             00000000
NumberOfRvaAndSizes     00000000

The Data Directory
Entry 0 00000000 00000000 Export Directory [.edata (or where ever we found it)]
Entry 1 00000000 00000000 Import Directory [parts of .idata]
Entry 2 00000000 00000000 Resource Directory [.rsrc]
Entry 3 00000000 00000000 Exception Directory [.pdata]
Entry 4 00000000 00000000 Security Directory
Entry 5 00000000 00000000 Base Relocation Directory [.reloc]
Entry 6 00000000 00000000 Debug Directory
Entry 7 00000000 00000000 Description Directory
Entry 8 00000000 00000000 Special Directory
Entry 9 00000000 00000000 Thread Storage Directory [.tls]
Entry a 00000000 00000000 Load Configuration Directory
Entry b 00000000 00000000 Bound Import Directory
Entry c 00000000 00000000 Import Address Table Directory
Entry d 00000000 00000000 Delay Import Directory
Entry e 00000000 00000000 CLR Runtime Header
Entry f 00000000 00000000 Reserved

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         000834fe  00000000  00000000  000000c0  2**0
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
[  0](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _binary_Tommy_jpg_start
[  1](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x000834fe _binary_Tommy_jpg_end
[  2](sec -1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x000834fe _binary_Tommy_jpg_size

The problem is that this last file (Tommy.o) loads properly, while the 1st file (Lilothyn.o) doesn't. As you can see the two are seemingly similar.

My code is extensive so i'll post the relevant parts only.

Source.cpp:

int displayFillThread(SDL_Renderer *renderer, SDL_Window *window, bool *_end) throw()
{
    std::cout << "Thread initializing..." << std::endl;
    SDL_Texture *texture = getTextureFromStream(&binary_Lilothyn_jpg_start, binary_Lilothyn_jpg_size, window, renderer);

    if (texture == NULL)
    {
        std::cout << "Thread: Failed to load image - " << SDL_GetError() << std::endl;
        return 1;
    }

    std::cout << "Thread initialized!" << std::endl;

    for(int i= 10; !*_end && i < 400; i+= 100)
        for(int j= 10; !*_end && j < 400; j+= 100)
        {
            SDL_Delay(100);
            AddTextureToRederer(renderer, texture, i, j, 100, 100);
            SDL_RenderPresent(renderer);
        }
    return 0;
}


SDL_Texture *getTextureFromStream(char binary[], unsigned int binary_size, SDL_Window *window, SDL_Renderer *renderer) throw()
{
    std::cout << "Starting getTextureFromJPEGStream!" << std::endl;
//  char* st = loadFileObject(binary_start, binary_end);

    SDL_RWops *rw = SDL_RWFromConstMem(binary, binary_size);

    std::cout << "rw loaded!" << std::endl;

    SDL_Surface *tmpSurf = IMG_Load_RW(rw, 0);
    //SDL_Surface *tmpSurf = SDL_CreateRGBSurfaceFrom(Lilothyn_jpg, 123, 102, 24, 96, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
    if (tmpSurf != NULL)
    {
        SDL_Surface *surf = SDL_ConvertSurfaceFormat(tmpSurf, SDL_GetWindowPixelFormat(window), 0);
        if (surf != NULL)
        {
            SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);

            SDL_FreeSurface(surf);

            std::cout << "tmpSurf loaded!" << std::endl;

            return texture;
        }//*/

//      SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, tmpSurf);

        SDL_FreeSurface(tmpSurf);

        std::cout << "surf load failed!" << std::endl;

        return NULL;
    }

    std::cout << "tmpSurf load failed!" << std::endl;

    return NULL;
}

int main(int argc, char** argv)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        std::cout << "SDL init Error: " << SDL_GetError() << std::endl;
        exit(1);
    }

    SDL_Window *window = SDL_CreateWindow("SDL_Test", 0, 0, _SCREEN_WIDTH_, _SCREEN_HEIGHT_, SDL_WINDOW_OPENGL | SDL_RENDERER_ACCELERATED);  //  SDL_WINDOW_FULLSCREEN |
    if (window == NULL)
    {
        std::cout << "Window init Error: " << SDL_GetError() << std::endl;
        exit(1);
    }

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    if (renderer == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
    }

    //SDL_Texture *texture = getTextureFromJPEGStream(binary_Tommy_jpg_start, binary_Tommy_jpg_end, binary_Tommy_jpg_size, window, renderer);

    SDL_Texture *texture = getTextureFromStream(&binary_Tommy_jpg_start, binary_Tommy_jpg_size, window, renderer);
            //getTextureFromImage("Resources/Tommy.jpg", window, renderer);
    if (texture == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Background Texture init error", SDL_GetError(), window);
        return 1;
    }

    SDL_RenderCopy(renderer, texture, NULL, NULL);
    //AddTextureToRederer(renderer, FTex, 10, 10, 100, 100);

    SDL_RenderPresent(renderer);

    SDL_Event event;
    bool _end = false;

    std::cout << "Starting Thread!" << std::endl;
    std::thread thread1;
    try
    {
        thread1 = std::thread(displayFillThread, renderer, window, &_end);
        std::cout << "Thread loaded!" << std::endl;
    }
    catch(std::exception& e)
    {
        std::cout << "Thread load failed: " << e.what() << std::endl;
        return 1;
    }

    std::cout << "main while starting!" << std::endl;
    while(!_end)
    {
//...

Source.h:

#ifndef SOURCE_H_
#define SOURCE_H_

static int _SCREEN_WIDTH_ = 1024;
static int _SCREEN_HEIGHT_ = 800;

extern char binary_Tommy_jpg_start;
extern unsigned int binary_Tommy_jpg_size;
extern char binary_Lilothyn_jpg_start;
extern unsigned int binary_Lilothyn_jpg_size;


int displayFillThread(SDL_Renderer *renderer, SDL_Window *window, bool *_end) throw();

SDL_Texture *getTextureFromStream(char binary[], unsigned int binary_size, SDL_Window *window, SDL_Renderer *renderer) throw();
//...

And the output:

Starting getTextureFromJPEGStream!
rw loaded!
tmpSurf loaded!
Starting Thread!
Thread loaded!
Thread initializing...
main while starting!

Note that it didn't write the second "getTextureFromJPEGStream!" neither did it appear to have thrown an exception. It compiles with no problems whatsoever so, is it possible that the Lilothyn.o file is being created corrupted? (I have rebuilt it more than once) And/or maybe the linker isn't linking correctly? While executing, the program simple stops responding after loading the 1st image, while trying to load the 2nd. Thank you for the replies.

c++
mingw
sdl-2
sdl-image
objcopy
asked on Stack Overflow Feb 14, 2016 by Lilothyn

1 Answer

0

I've solved this now, the problem was the types used on the .o file. New code is as follows.

Source.h:

extern uint8_t binary_Lilothyn_jpg_start[];
extern uint8_t binary_Lilothyn_jpg_size[];

Source.cpp

SDL_Texture *getTextureFromStream(uint8_t binary[], uint8_t binary_size[], SDL_Window *window, SDL_Renderer *renderer) throw()
{
    size_t converted_size = (size_t)((void *)binary_size);
    SDL_Surface *tmpSurf = IMG_Load_RW(SDL_RWFromConstMem(binary, converted_size), 0);
    if (tmpSurf != NULL)
    {
//...

Thanks to all the viewers, I hope this helps!

answered on Stack Overflow Feb 14, 2016 by Lilothyn

User contributions licensed under CC BY-SA 3.0