Skip to content

Commit 8abcf66

Browse files
committed
windows: Enable GC and implement bss start and end symbols
The pointers to the bss section are acquired in init.c() by inspecting the PE header. Works for msvc and mingw.
1 parent a96cc82 commit 8abcf66

7 files changed

Lines changed: 115 additions & 4 deletions

File tree

unix/gccollect.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,11 @@ void gc_collect(void) {
130130

131131
gc_collect_start();
132132
// this traces the .bss section
133-
#ifdef __CYGWIN__
133+
#if defined( __CYGWIN__ )
134134
#define BSS_START __bss_start__
135+
#elif defined( _MSC_VER ) || defined( __MINGW32__ )
136+
#define BSS_START *bss_start
137+
#define _end *bss_end
135138
#else
136139
#define BSS_START __bss_start
137140
#endif
@@ -141,7 +144,16 @@ void gc_collect(void) {
141144
regs_t regs;
142145
gc_helper_get_regs(regs);
143146
// GC stack (and regs because we captured them)
144-
gc_collect_root((void**)&regs, ((machine_uint_t)stack_top - (machine_uint_t)&regs) / sizeof(machine_uint_t));
147+
#ifdef __MINGW32__
148+
// The Mingw cross-compiler on Travis complains
149+
// 'warning: dereferencing type-punned pointer will break strict-aliasing rules'
150+
// when casting &regs to void** directly so use a union.
151+
union { regs_t *r; void **ptr; } cast_regs = { &regs };
152+
void **regs_ptr = cast_regs.ptr;
153+
#else
154+
void **regs_ptr = (void**)&regs;
155+
#endif
156+
gc_collect_root(regs_ptr, ((machine_uint_t)stack_top - (machine_uint_t)&regs) / sizeof(machine_uint_t));
145157
gc_collect_end();
146158

147159
//printf("-----\n");

windows/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ SRC_C = \
3535
unix/file.c \
3636
unix/input.c \
3737
unix/modtime.c \
38+
unix/gccollect.c \
3839
realpath.c \
3940
init.c \
4041
sleep.c \
42+
bss.c \
4143

4244
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
4345

windows/bss.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

windows/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
#include <stdio.h>
2929
#include <windows.h>
3030

31+
extern void getbss();
32+
3133
HANDLE hSleepEvent = NULL;
3234

3335
void init() {
36+
getbss();
3437
hSleepEvent = CreateEvent(NULL, TRUE, FALSE, FALSE);
3538
#ifdef __MINGW32__
3639
putenv("PRINTF_EXPONENT_DIGITS=2");

windows/mpconfigport.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
#define MICROPY_PY_CMATH (1)
4444
#define MICROPY_PY_SYS_STDFILES (1)
4545
#define MICROPY_PY_SYS_EXIT (1)
46+
#define MICROPY_ENABLE_GC (1)
47+
#define MICROPY_ENABLE_FINALISER (1)
48+
#define MICROPY_PY_GC_COLLECT_RETVAL (1)
49+
#ifdef _MSC_VER
50+
#define MICROPY_GCREGS_SETJMP (1)
51+
#endif
4652
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
4753
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
4854
#define MICROPY_PORT_INIT_FUNC init()
@@ -113,6 +119,14 @@ void msec_sleep(double msec);
113119
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
114120

115121

122+
// Put static/global variables in sections with a known name we can lookup for the GC
123+
// For this to work this header must be included by all sources, which is the case normally
124+
#define MICROPY_PORT_DATASECTION "upydata"
125+
#define MICROPY_PORT_BSSSECTION "upybss"
126+
#pragma data_seg(MICROPY_PORT_DATASECTION)
127+
#pragma bss_seg(MICROPY_PORT_BSSSECTION)
128+
129+
116130
// System headers (needed e.g. for nlr.h)
117131

118132
#include <stddef.h> //for NULL
@@ -122,3 +136,8 @@ void msec_sleep(double msec);
122136

123137
int snprintf(char *dest, size_t count, const char *format, ...);
124138
#endif
139+
140+
// MingW specifics
141+
#ifdef __MINGW32__
142+
#define MICROPY_PORT_BSSSECTION ".bss"
143+
#endif

windows/msvc/common.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
</ClCompile>
1717
<Link>
1818
<GenerateDebugInformation>true</GenerateDebugInformation>
19+
<GenerateMapFile>true</GenerateMapFile>
1920
</Link>
2021
</ItemDefinitionGroup>
2122
<ItemGroup />
22-
</Project>
23+
</Project>

windows/msvc/sources.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<ClCompile Include="$(PyBaseDir)py\*.c" />
8-
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c" />
8+
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c;$(PyBaseDir)unix\seg_helpers.c" />
99
<ClCompile Include="$(PyBaseDir)windows\*.c" />
1010
<ClCompile Include="$(PyBaseDir)windows\msvc\*.c" />
1111
</ItemGroup>

0 commit comments

Comments
 (0)