|
| 1 | +/* |
| 2 | +* This file is part of the Micro Python project, http://micropython.org/ |
| 3 | +* |
| 4 | +* The MIT License (MIT) |
| 5 | +* |
| 6 | +* Copyright (c) 2013, 2014 Damien P. George |
| 7 | +* |
| 8 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +* of this software and associated documentation files (the "Software"), to deal |
| 10 | +* in the Software without restriction, including without limitation the rights |
| 11 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +* copies of the Software, and to permit persons to whom the Software is |
| 13 | +* furnished to do so, subject to the following conditions: |
| 14 | +* |
| 15 | +* The above copyright notice and this permission notice shall be included in |
| 16 | +* all copies or substantial portions of the Software. |
| 17 | +* |
| 18 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +* THE SOFTWARE. |
| 25 | +*/ |
| 26 | + |
| 27 | +#include "mpconfig.h" |
| 28 | +#include "misc.h" |
| 29 | +#include "nlr.h" |
| 30 | +#include "qstr.h" |
| 31 | +#include "obj.h" |
| 32 | +#include <windows.h> |
| 33 | + |
| 34 | +IMAGE_NT_HEADERS *header_from_memory(const char *module) { |
| 35 | + BYTE *base_addr = (BYTE*)GetModuleHandleA(module); |
| 36 | + IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER*)base_addr; |
| 37 | + return (IMAGE_NT_HEADERS*)(base_addr + dos_header->e_lfanew); |
| 38 | +} |
| 39 | + |
| 40 | +IMAGE_SECTION_HEADER *find_section(IMAGE_NT_HEADERS *nt_header, const char *name) { |
| 41 | + int i; |
| 42 | + IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(nt_header); |
| 43 | + for (i = 0; i < nt_header->FileHeader.NumberOfSections; ++i) { |
| 44 | + if (strcmp((const char *)section->Name, name) == 0) { |
| 45 | + return section; |
| 46 | + } |
| 47 | + ++section; |
| 48 | + } |
| 49 | + return NULL; |
| 50 | +} |
| 51 | + |
| 52 | +void section_boundaries(IMAGE_NT_HEADERS *nt_header, IMAGE_SECTION_HEADER *section, char **start, char **end) { |
| 53 | + if (section == NULL) { |
| 54 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Could not lookup section boundaries")); |
| 55 | + } |
| 56 | + *start = (char*)(nt_header->OptionalHeader.ImageBase + section->VirtualAddress); |
| 57 | + *end = *start + section->Misc.VirtualSize; |
| 58 | +} |
| 59 | + |
| 60 | +void section_boundaries_from_module(const char *module, const char *section, char **start, char **end) { |
| 61 | + IMAGE_NT_HEADERS *nt_header = header_from_memory(module); |
| 62 | + IMAGE_SECTION_HEADER *dsection = find_section(nt_header, section); |
| 63 | + section_boundaries(nt_header, dsection, start, end); |
| 64 | +} |
| 65 | + |
| 66 | +char *bss_start = 0; |
| 67 | +char *bss_end = 0; |
| 68 | + |
| 69 | +//MSVC has no __bss_start and _end but we can get accurate section info from the PE header. |
| 70 | +//The standard .bss section is appended to the standard .data section however so it cannot |
| 71 | +//be looked up by name. To deal with that we put all uPy static variables in a named section. |
| 72 | +void getbss() { |
| 73 | + section_boundaries_from_module(NULL, MICROPY_PORT_BSSSECTION, &bss_start, &bss_end); |
| 74 | +} |
0 commit comments