Skip to content

Commit 70f0460

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
authored andcommitted
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 <franziska.hinkelmann@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com>
1 parent 755da03 commit 70f0460

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
@@ -313,6 +313,19 @@
313313
},
314314

315315
'targets': [
316+
{
317+
'target_name': 'node_text_start',
318+
'type': 'none',
319+
'conditions': [
320+
[ 'OS=="linux" and '
321+
'target_arch=="x64"', {
322+
'type': 'static_library',
323+
'sources': [
324+
'src/large_pages/node_text_start.S'
325+
]
326+
}],
327+
]
328+
},
316329
{
317330
'target_name': '<(node_core_target_name)',
318331
'type': 'executable',
@@ -498,6 +511,13 @@
498511
'src/node_snapshot_stub.cc'
499512
],
500513
}],
514+
[ 'OS=="linux" and '
515+
'target_arch=="x64"', {
516+
'dependencies': [ 'node_text_start' ],
517+
'ldflags+': [
518+
'<(PRODUCT_DIR)/obj.target/node_text_start/src/large_pages/node_text_start.o'
519+
]
520+
}],
501521
],
502522
}, # node_core_target_name
503523
{

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)