From 15e6e8cf3e6b06a79968c2c4d3eadad5729f74e1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 3 Apr 2014 10:45:36 -0700 Subject: rustc: Stop using LLVMGetSectionName The recent pull request to remove libc from libstd has hit a wall in compiling on windows, and I've been trying to investigate on the try bots as to why (it compiles locally just fine). To the best of my knowledge, the LLVM section iterator is behaving badly when iterating over the sections of the libc DLL. Upon investigating the LLVMGetSectionName function in LLVM, I discovered that this function doesn't always return a null-terminated string. It returns the data pointer of a StringRef instance (LLVM's equivalent of &str essentially), but it has no method of returning the length of the name of the section. This commit modifies the section iteration when loading libraries to invoke a custom LLVMRustGetSectionName which will correctly return both the length and the data pointer. I have not yet verified that this will fix landing liblibc, as it will require a snapshot before doing a full test. Regardless, this is a worrisome situation regarding the LLVM API, and should likely be fixed anyway. --- src/rustllvm/RustWrapper.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 4755c7873fc..5a00a8034e6 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -10,6 +10,7 @@ #include "rustllvm.h" #include "llvm/Object/Archive.h" +#include "llvm/Object/ObjectFile.h" //===----------------------------------------------------------------------=== // @@ -679,3 +680,27 @@ extern "C" int LLVMVersionMinor() { return LLVM_VERSION_MINOR; } + +// Note that the two following functions look quite similar to the +// LLVMGetSectionName function. Sadly, it appears that this function only +// returns a char* pointer, which isn't guaranteed to be null-terminated. The +// function provided by LLVM doesn't return the length, so we've created our own +// function which returns the length as well as the data pointer. +// +// For an example of this not returning a null terminated string, see +// lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the +// branches explicitly creates a StringRef without a null terminator, and then +// that's returned. + +inline section_iterator *unwrap(LLVMSectionIteratorRef SI) { + return reinterpret_cast(SI); +} + +extern "C" int +LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) { + StringRef ret; + if (error_code ec = (*unwrap(SI))->getName(ret)) + report_fatal_error(ec.message()); + *ptr = ret.data(); + return ret.size(); +} -- cgit 1.4.1-3-g733a5