Skip to content

Commit e5a743c

Browse files
author
Siva Chandra Reddy
committed
Add implementations of POSIX mmap and munmap functions.
Summary: A set of of linux x86_64 internal syscall helpers have also been added. This change does not try to be perfect with respect to OS and machine abstractions. A TODO note has been added at places where such abstractions would help and make the arrangement scalable and cleaner. Addressing the TODOs and building such abstractions is not in the scope of this change. It is hoped that follow up changes cleaning up the problem areas and addressing the TODOs will better illustrate the need for the changes. This change also does not try to imitate mmap and munmap implementations of other libcs. The idea here is to put in the bare minimum required to obtain a working mmap and munmap, and then add the rest of the functionality on an as needed basis. Reviewers: abrachet, phosek, stanshebs, theraven Subscribers: mgorny, MaskRay, jfb, libc-commits Tags: #libc-project Differential Revision: https://reviews.llvm.org/D71634
1 parent 187f66b commit e5a743c

31 files changed

+2916
-35
lines changed

libc/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
1919
include(CMakeParseArguments)
2020
include(LLVMLibCRules)
2121

22-
add_subdirectory(include)
2322
add_subdirectory(src)
23+
add_subdirectory(config)
24+
add_subdirectory(include)
2425
add_subdirectory(lib)
2526
add_subdirectory(utils)

libc/cmake/modules/LLVMLibCRules.cmake

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function(add_redirector_object target_name)
250250
)
251251
endfunction(add_redirector_object)
252252

253-
# Rule to build a shared library of redirector objects
253+
# Rule to build a shared library of redirector objects.
254254
function(add_redirector_library target_name)
255255
cmake_parse_arguments(
256256
"REDIRECTOR_LIBRARY"
@@ -287,6 +287,15 @@ function(add_redirector_library target_name)
287287
)
288288
endfunction(add_redirector_library)
289289

290+
# Rule to add a gtest unittest.
291+
# Usage
292+
# add_libc_unittest(
293+
# <target name>
294+
# SUITE <name of the suite this test belongs to>
295+
# SRCS <list of .cpp files for the test>
296+
# HDRS <list of .h files for the test>
297+
# DEPENDS <list of dependencies>
298+
# )
290299
function(add_libc_unittest target_name)
291300
if(NOT LLVM_INCLUDE_TESTS)
292301
return()
@@ -306,15 +315,19 @@ function(add_libc_unittest target_name)
306315
message(FATAL_ERROR "'add_libc_unittest' target requires a DEPENDS list of 'add_entrypoint_object' targets.")
307316
endif()
308317

309-
set(entrypoint_objects "")
318+
set(library_deps "")
310319
foreach(dep IN LISTS LIBC_UNITTEST_DEPENDS)
311320
get_target_property(dep_type ${dep} "TARGET_TYPE")
312-
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
313-
if(NOT dep_is_entrypoint)
314-
message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_unittest' is not an 'add_entrypoint_object' target.")
321+
if (dep_type)
322+
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
323+
if(dep_is_entrypoint)
324+
get_target_property(obj_file ${dep} "OBJECT_FILE_RAW")
325+
list(APPEND library_deps ${obj_file})
326+
continue()
327+
endif()
315328
endif()
316-
get_target_property(obj_file ${dep} "OBJECT_FILE_RAW")
317-
list(APPEND entrypoint_objects "${obj_file}")
329+
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
330+
# to the list of library_deps.
318331
endforeach(dep)
319332

320333
add_executable(
@@ -329,15 +342,23 @@ function(add_libc_unittest target_name)
329342
${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
330343
${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include
331344
${LIBC_SOURCE_DIR}
345+
${LIBC_BUILD_DIR}
332346
)
333-
target_link_libraries(${target_name} PRIVATE ${entrypoint_objects} gtest_main gtest)
347+
348+
if(library_deps)
349+
target_link_libraries(${target_name} PRIVATE ${library_deps})
350+
endif()
351+
334352
set_target_properties(${target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
335353

336354
add_dependencies(
337355
${target_name}
338356
${LIBC_UNITTEST_DEPENDS}
339357
gtest
340358
)
359+
360+
target_link_libraries(${target_name} PRIVATE gtest_main gtest)
361+
341362
add_custom_command(
342363
TARGET ${target_name}
343364
POST_BUILD

libc/config/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(linux)

libc/config/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(x86_64)

libc/config/linux/api.td

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,20 @@ include "spec/linux.td"
44
include "spec/posix.td"
55
include "spec/stdc.td"
66

7-
def FloatT : TypeDecl<"float_t"> {
8-
let Decl = [{
9-
#if __FLT_EVAL_METHOD__ == 1
10-
typedef float float_t
11-
#elif __FLT_EVAL_METHOD__ == 2
12-
...
13-
#else
14-
...
15-
#endif
16-
}]; // This is only an example and not exactly how it will appear
17-
}
18-
197
def SizeT : TypeDecl<"size_t"> {
208
let Decl = [{
219
#define __need_size_t
2210
#include <stddef.h>
2311
}];
2412
}
2513

14+
def OffT : TypeDecl<"off_t"> {
15+
let Decl = [{
16+
#define __need_off_t
17+
#include <__posix-types.h>
18+
}];
19+
}
20+
2621
def NullMacro : MacroDef<"NULL"> {
2722
let Defn = [{
2823
#define __need_NULL
@@ -42,10 +37,6 @@ def MathAPI : PublicAPI<"math.h"> {
4237
"acos",
4338
"acosl",
4439
];
45-
46-
let TypeDeclarations = [
47-
FloatT,
48-
];
4940
}
5041

5142
def StringAPI : PublicAPI<"string.h"> {
@@ -111,3 +102,34 @@ def ErrnoAPI : PublicAPI<"errno.h"> {
111102
MacroDefineIfNot<"EHWPOISON", "133">,
112103
];
113104
}
105+
106+
def SysMManAPI : PublicAPI<"sys/mman.h"> {
107+
let Macros = [
108+
SimpleMacroDef<"PROT_NONE", "0">,
109+
SimpleMacroDef<"PROT_READ", "1">,
110+
SimpleMacroDef<"PROT_WRITE", "2">,
111+
SimpleMacroDef<"PROT_EXEC", "4">,
112+
113+
SimpleMacroDef<"MAP_FIXED", "1">,
114+
SimpleMacroDef<"MAP_PRIVATE", "2">,
115+
SimpleMacroDef<"MAP_SHARED", "4">,
116+
117+
SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
118+
119+
// TODO: The value of 0x20 is good for x86_64, but has to be extended
120+
// in some manner to accommodate other machine architectures.
121+
SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
122+
123+
// TODO: Add other MAP_* macros used by Linux.
124+
];
125+
126+
let TypeDeclarations = [
127+
SizeT,
128+
OffT,
129+
];
130+
131+
let Functions = [
132+
"mmap",
133+
"munmap",
134+
];
135+
}

libc/src/__support/linux/entrypoint_macro.h.inc renamed to libc/config/linux/platfrom_defs.h.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===---- Definition of LLVM_LIBC_ENTRYPOINT macro for ELF paltforms ----*-===//
1+
//===----- Definition of platform specific macros for ELF paltforms -------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -11,3 +11,9 @@
1111
#define ENTRYPOINT_SECTION_ATTRIBUTE(name) \
1212
__attribute__((section(".llvm.libc.entrypoint."#name)))
1313
#define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name
14+
15+
// TODO: Get rid of the PAGE_SIZE macro. It is present only as an interim
16+
// measure until we can move the implementations of mmap and munmap to under
17+
// the config/linux directory. After that, the implementations can use
18+
// EXEC_PAGESIZE until page size can be read from the aux vector.
19+
#define PAGE_SIZE 4096

0 commit comments

Comments
 (0)