Skip to content

Commit f2572a1

Browse files
author
Gabriel Schulhof
committed
src: start the .text section with an asm symbol
We create an object file in assembly which introduces the symbol `__node_text_start` into the .text section and place the resulting object file as the first file the linker encounters. We do this to ensure that we can recognize the boundaries of the .text section when attempting to establish the address range to map to large pages. Additionally, we rename the section containing the remapping code from `.lpstub` to `lpstub` so as to take advantage of the linker's feature whereby it inserts the symbol `__start_lpstub` when the section's name can be rendered as a valid C variable. We need this symbol in order to avoid self-mapping the remapping code to large pages, because doing so would cause the process to crash. PR-URL: #31981 Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: David Carlier <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
1 parent 2f3d446 commit f2572a1

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

node.gyp

+20
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@
307307
},
308308

309309
'targets': [
310+
{
311+
'target_name': 'node_text_start',
312+
'type': 'none',
313+
'conditions': [
314+
[ 'OS=="linux" and '
315+
'target_arch=="x64"', {
316+
'type': 'static_library',
317+
'sources': [
318+
'src/large_pages/node_text_start.S'
319+
]
320+
}],
321+
]
322+
},
310323
{
311324
'target_name': '<(node_core_target_name)',
312325
'type': 'executable',
@@ -492,6 +505,13 @@
492505
'src/node_snapshot_stub.cc'
493506
],
494507
}],
508+
[ 'OS=="linux" and '
509+
'target_arch=="x64"', {
510+
'dependencies': [ 'node_text_start' ],
511+
'ldflags+': [
512+
'<(PRODUCT_DIR)/obj.target/node_text_start/src/large_pages/node_text_start.o'
513+
]
514+
}],
495515
],
496516
}, # node_core_target_name
497517
{

src/large_pages/node_large_page.cc

+14-14
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@
7171

7272
#if defined(__linux__)
7373
extern "C" {
74-
extern char __executable_start;
74+
// This symbol must be declared weak because this file becomes part of all
75+
// Node.js targets (like node_mksnapshot, node_mkcodecache, and cctest) and
76+
// those files do not supply the symbol.
77+
extern char __attribute__((weak)) __node_text_start;
78+
extern char __start_lpstub;
7579
} // extern "C"
7680
#endif // defined(__linux__)
7781

@@ -121,6 +125,8 @@ struct text_region FindNodeTextRegion() {
121125
std::string dev;
122126
char dash;
123127
uintptr_t start, end, offset, inode;
128+
uintptr_t node_text_start = reinterpret_cast<uintptr_t>(&__node_text_start);
129+
uintptr_t lpstub_start = reinterpret_cast<uintptr_t>(&__start_lpstub);
124130

125131
ifs.open("/proc/self/maps");
126132
if (!ifs) {
@@ -144,21 +150,15 @@ struct text_region FindNodeTextRegion() {
144150
std::string pathname;
145151
iss >> pathname;
146152

147-
if (start != reinterpret_cast<uintptr_t>(&__executable_start))
153+
if (permission != "r-xp")
148154
continue;
149155

150-
// The next line is our .text section.
151-
if (!std::getline(ifs, map_line))
152-
break;
153-
154-
iss = std::istringstream(map_line);
155-
iss >> std::hex >> start;
156-
iss >> dash;
157-
iss >> std::hex >> end;
158-
iss >> permission;
156+
if (node_text_start < start || node_text_start >= end)
157+
continue;
159158

160-
if (permission != "r-xp")
161-
break;
159+
start = node_text_start;
160+
if (lpstub_start > start && lpstub_start <= end)
161+
end = lpstub_start;
162162

163163
char* from = reinterpret_cast<char*>(hugepage_align_up(start));
164164
char* to = reinterpret_cast<char*>(hugepage_align_down(end));
@@ -318,7 +318,7 @@ static bool IsSuperPagesEnabled() {
318318
// d. If successful copy the code there and unmap the original region
319319
int
320320
#if !defined(__APPLE__)
321-
__attribute__((__section__(".lpstub")))
321+
__attribute__((__section__("lpstub")))
322322
#else
323323
__attribute__((__section__("__TEXT,__lpstub")))
324324
#endif

src/large_pages/node_text_start.S

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.align 0x2000
3+
.global __node_text_start
4+
.hidden __node_text_start
5+
__node_text_start:

0 commit comments

Comments
 (0)