2003-07-24 22:20:58 +02:00
|
|
|
//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-07-24 22:20:58 +02:00
|
|
|
//
|
2010-01-16 22:08:46 +01:00
|
|
|
// Unified name mangler for assembly backends.
|
2003-07-24 22:20:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-07 22:19:40 +01:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2021-01-23 08:25:03 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-03-31 06:46:50 +02:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2015-06-23 15:59:29 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
2010-03-12 22:03:47 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-14 22:35:53 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2015-06-23 16:11:09 +02:00
|
|
|
namespace {
|
|
|
|
enum ManglerPrefixTy {
|
|
|
|
Default, ///< Emit default string before each symbol.
|
|
|
|
Private, ///< Emit "private" prefix before each symbol.
|
|
|
|
LinkerPrivate ///< Emit "linker private" prefix before each symbol.
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-06-23 14:21:54 +02:00
|
|
|
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
|
2015-06-23 16:11:09 +02:00
|
|
|
ManglerPrefixTy PrefixTy,
|
2015-06-23 14:21:54 +02:00
|
|
|
const DataLayout &DL, char Prefix) {
|
2010-01-13 08:01:09 +01:00
|
|
|
SmallString<256> TmpData;
|
2010-01-13 13:45:23 +01:00
|
|
|
StringRef Name = GVName.toStringRef(TmpData);
|
2010-01-13 08:01:09 +01:00
|
|
|
assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
|
2014-01-03 20:21:54 +01:00
|
|
|
|
2014-08-01 16:16:40 +02:00
|
|
|
// No need to do anything special if the global has the special "do not
|
|
|
|
// mangle" flag in the name.
|
|
|
|
if (Name[0] == '\1') {
|
|
|
|
OS << Name.substr(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 21:13:32 +01:00
|
|
|
if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
|
|
|
|
Prefix = '\0';
|
|
|
|
|
2015-06-23 16:11:09 +02:00
|
|
|
if (PrefixTy == Private)
|
2014-01-29 03:30:38 +01:00
|
|
|
OS << DL.getPrivateGlobalPrefix();
|
2015-06-23 16:11:09 +02:00
|
|
|
else if (PrefixTy == LinkerPrivate)
|
2014-01-29 03:30:38 +01:00
|
|
|
OS << DL.getLinkerPrivateGlobalPrefix();
|
2010-01-17 19:22:35 +01:00
|
|
|
|
2014-10-28 02:29:26 +01:00
|
|
|
if (Prefix != '\0')
|
|
|
|
OS << Prefix;
|
2013-11-13 15:01:59 +01:00
|
|
|
|
2010-01-17 20:23:46 +01:00
|
|
|
// If this is a simple string that doesn't need escaping, just append it.
|
2014-01-29 03:30:38 +01:00
|
|
|
OS << Name;
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:11:09 +02:00
|
|
|
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
|
|
|
|
const DataLayout &DL,
|
|
|
|
ManglerPrefixTy PrefixTy) {
|
2015-06-23 14:21:54 +02:00
|
|
|
char Prefix = DL.getGlobalPrefix();
|
2015-06-23 15:59:29 +02:00
|
|
|
return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
|
2014-01-29 03:30:38 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 16:11:09 +02:00
|
|
|
void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
|
|
|
|
const DataLayout &DL) {
|
|
|
|
return getNameWithPrefixImpl(OS, GVName, DL, Default);
|
|
|
|
}
|
|
|
|
|
2014-01-29 03:30:38 +01:00
|
|
|
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
|
2015-06-23 16:11:09 +02:00
|
|
|
const Twine &GVName, const DataLayout &DL) {
|
2014-01-29 03:30:38 +01:00
|
|
|
raw_svector_ostream OS(OutName);
|
2015-06-23 15:59:29 +02:00
|
|
|
char Prefix = DL.getGlobalPrefix();
|
2015-06-23 16:11:09 +02:00
|
|
|
return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
|
2010-01-13 08:01:09 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:29:26 +01:00
|
|
|
static bool hasByteCountSuffix(CallingConv::ID CC) {
|
|
|
|
switch (CC) {
|
|
|
|
case CallingConv::X86_FastCall:
|
|
|
|
case CallingConv::X86_StdCall:
|
|
|
|
case CallingConv::X86_VectorCall:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Microsoft fastcall and stdcall functions require a suffix on their name
|
|
|
|
/// indicating the number of words of arguments they take.
|
|
|
|
static void addByteCountSuffix(raw_ostream &OS, const Function *F,
|
2015-03-10 03:37:25 +01:00
|
|
|
const DataLayout &DL) {
|
2010-03-12 22:03:47 +01:00
|
|
|
// Calculate arguments size total.
|
|
|
|
unsigned ArgWords = 0;
|
2020-06-26 17:36:22 +02:00
|
|
|
|
|
|
|
const unsigned PtrSize = DL.getPointerSize();
|
|
|
|
|
2021-02-27 19:09:25 +01:00
|
|
|
for (const Argument &A : F->args()) {
|
2014-02-01 00:50:57 +01:00
|
|
|
// 'Dereference' type in case of byval or inalloca parameter attribute.
|
2021-02-27 19:09:25 +01:00
|
|
|
uint64_t AllocSize = A.hasPassPointeeByValueCopyAttr() ?
|
|
|
|
A.getPassPointeeByValueCopySize(DL) :
|
|
|
|
DL.getTypeAllocSize(A.getType());
|
2020-06-26 17:36:22 +02:00
|
|
|
|
2014-10-28 02:29:26 +01:00
|
|
|
// Size should be aligned to pointer size.
|
2020-06-26 17:36:22 +02:00
|
|
|
ArgWords += alignTo(AllocSize, PtrSize);
|
2010-03-12 22:03:47 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 03:30:38 +01:00
|
|
|
OS << '@' << ArgWords;
|
|
|
|
}
|
2009-09-11 07:40:42 +02:00
|
|
|
|
2014-02-19 18:23:20 +01:00
|
|
|
void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
|
2015-03-17 21:40:21 +01:00
|
|
|
bool CannotUsePrivateLabel) const {
|
2015-06-23 16:11:09 +02:00
|
|
|
ManglerPrefixTy PrefixTy = Default;
|
2015-03-17 21:40:21 +01:00
|
|
|
if (GV->hasPrivateLinkage()) {
|
2014-02-19 18:23:20 +01:00
|
|
|
if (CannotUsePrivateLabel)
|
2015-06-23 16:11:09 +02:00
|
|
|
PrefixTy = LinkerPrivate;
|
2014-02-19 18:23:20 +01:00
|
|
|
else
|
2015-06-23 16:11:09 +02:00
|
|
|
PrefixTy = Private;
|
2014-02-19 18:23:20 +01:00
|
|
|
}
|
2014-01-14 12:53:26 +01:00
|
|
|
|
2015-06-23 15:59:29 +02:00
|
|
|
const DataLayout &DL = GV->getParent()->getDataLayout();
|
2014-01-29 03:30:38 +01:00
|
|
|
if (!GV->hasName()) {
|
2010-03-12 22:03:47 +01:00
|
|
|
// Get the ID for the global, assigning a new one if we haven't got one
|
|
|
|
// already.
|
|
|
|
unsigned &ID = AnonGlobalIDs[GV];
|
2014-01-29 03:30:38 +01:00
|
|
|
if (ID == 0)
|
2016-09-29 04:03:50 +02:00
|
|
|
ID = AnonGlobalIDs.size();
|
2014-01-29 03:30:38 +01:00
|
|
|
|
2010-03-12 22:03:47 +01:00
|
|
|
// Must mangle the global into a unique ID.
|
2015-06-23 16:11:09 +02:00
|
|
|
getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
|
2014-01-29 03:30:38 +01:00
|
|
|
return;
|
2010-03-12 22:03:47 +01:00
|
|
|
}
|
2014-01-03 20:21:54 +01:00
|
|
|
|
2014-01-29 03:30:38 +01:00
|
|
|
StringRef Name = GV->getName();
|
2015-06-23 15:59:29 +02:00
|
|
|
char Prefix = DL.getGlobalPrefix();
|
2014-10-28 02:29:26 +01:00
|
|
|
|
|
|
|
// Mangle functions with Microsoft calling conventions specially. Only do
|
|
|
|
// this mangling for x86_64 vectorcall and 32-bit x86.
|
|
|
|
const Function *MSFunc = dyn_cast<Function>(GV);
|
2018-03-16 21:13:32 +01:00
|
|
|
|
|
|
|
// Don't add byte count suffixes when '\01' or '?' are in the first
|
|
|
|
// character.
|
|
|
|
if (Name.startswith("\01") ||
|
|
|
|
(DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
|
|
|
|
MSFunc = nullptr;
|
|
|
|
|
2014-10-28 14:12:13 +01:00
|
|
|
CallingConv::ID CC =
|
|
|
|
MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
|
2015-06-23 15:59:29 +02:00
|
|
|
if (!DL.hasMicrosoftFastStdCallMangling() &&
|
2014-10-28 02:29:26 +01:00
|
|
|
CC != CallingConv::X86_VectorCall)
|
|
|
|
MSFunc = nullptr;
|
|
|
|
if (MSFunc) {
|
|
|
|
if (CC == CallingConv::X86_FastCall)
|
|
|
|
Prefix = '@'; // fastcall functions have an @ prefix instead of _.
|
|
|
|
else if (CC == CallingConv::X86_VectorCall)
|
|
|
|
Prefix = '\0'; // vectorcall functions have no prefix.
|
2010-03-12 22:03:47 +01:00
|
|
|
}
|
2014-01-29 03:30:38 +01:00
|
|
|
|
2015-06-23 15:59:29 +02:00
|
|
|
getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
|
2014-01-29 03:30:38 +01:00
|
|
|
|
|
|
|
if (!MSFunc)
|
|
|
|
return;
|
|
|
|
|
2014-10-28 02:29:26 +01:00
|
|
|
// If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
|
|
|
|
// or vectorcall, add it. These functions have a suffix of @N where N is the
|
|
|
|
// cumulative byte size of all of the parameters to the function in decimal.
|
|
|
|
if (CC == CallingConv::X86_VectorCall)
|
|
|
|
OS << '@'; // vectorcall functions use a double @ suffix.
|
2014-01-29 03:30:38 +01:00
|
|
|
FunctionType *FT = MSFunc->getFunctionType();
|
2014-10-28 02:29:26 +01:00
|
|
|
if (hasByteCountSuffix(CC) &&
|
2014-01-29 03:30:38 +01:00
|
|
|
// "Pure" variadic functions do not receive @0 suffix.
|
|
|
|
(!FT->isVarArg() || FT->getNumParams() == 0 ||
|
|
|
|
(FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
|
2015-06-23 15:59:29 +02:00
|
|
|
addByteCountSuffix(OS, MSFunc, DL);
|
2014-01-29 03:30:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
|
2014-02-19 18:23:20 +01:00
|
|
|
const GlobalValue *GV,
|
2015-03-17 21:40:21 +01:00
|
|
|
bool CannotUsePrivateLabel) const {
|
2014-01-29 03:30:38 +01:00
|
|
|
raw_svector_ostream OS(OutName);
|
2015-03-17 21:40:21 +01:00
|
|
|
getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
|
2009-09-11 07:40:42 +02:00
|
|
|
}
|
2017-03-31 06:46:50 +02:00
|
|
|
|
2020-10-15 22:57:21 +02:00
|
|
|
// Check if the name needs quotes to be safe for the linker to interpret.
|
|
|
|
static bool canBeUnquotedInDirective(char C) {
|
2021-01-23 08:25:03 +01:00
|
|
|
return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '@';
|
2020-10-15 22:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool canBeUnquotedInDirective(StringRef Name) {
|
|
|
|
if (Name.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If any of the characters in the string is an unacceptable character, force
|
|
|
|
// quotes.
|
|
|
|
for (char C : Name) {
|
|
|
|
if (!canBeUnquotedInDirective(C))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-31 06:46:50 +02:00
|
|
|
void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
|
|
|
|
const Triple &TT, Mangler &Mangler) {
|
|
|
|
if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
|
|
|
|
return;
|
|
|
|
|
2019-07-08 23:05:20 +02:00
|
|
|
if (TT.isWindowsMSVCEnvironment())
|
2017-03-31 06:46:50 +02:00
|
|
|
OS << " /EXPORT:";
|
|
|
|
else
|
|
|
|
OS << " -export:";
|
|
|
|
|
2020-10-15 22:57:21 +02:00
|
|
|
bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
|
|
|
|
if (NeedQuotes)
|
|
|
|
OS << "\"";
|
2017-03-31 06:46:50 +02:00
|
|
|
if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
|
|
|
|
std::string Flag;
|
|
|
|
raw_string_ostream FlagOS(Flag);
|
|
|
|
Mangler.getNameWithPrefix(FlagOS, GV, false);
|
|
|
|
FlagOS.flush();
|
|
|
|
if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
|
|
|
|
OS << Flag.substr(1);
|
|
|
|
else
|
|
|
|
OS << Flag;
|
|
|
|
} else {
|
|
|
|
Mangler.getNameWithPrefix(OS, GV, false);
|
|
|
|
}
|
2020-10-15 22:57:21 +02:00
|
|
|
if (NeedQuotes)
|
|
|
|
OS << "\"";
|
2017-03-31 06:46:50 +02:00
|
|
|
|
|
|
|
if (!GV->getValueType()->isFunctionTy()) {
|
2019-07-08 23:05:20 +02:00
|
|
|
if (TT.isWindowsMSVCEnvironment())
|
2017-03-31 06:46:50 +02:00
|
|
|
OS << ",DATA";
|
|
|
|
else
|
|
|
|
OS << ",data";
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 01:28:02 +01:00
|
|
|
|
|
|
|
void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
|
|
|
|
const Triple &T, Mangler &M) {
|
2019-07-08 23:05:20 +02:00
|
|
|
if (!T.isWindowsMSVCEnvironment())
|
2018-01-20 01:28:02 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
OS << " /INCLUDE:";
|
2020-10-15 22:57:21 +02:00
|
|
|
bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
|
|
|
|
if (NeedQuotes)
|
|
|
|
OS << "\"";
|
2018-01-20 01:28:02 +01:00
|
|
|
M.getNameWithPrefix(OS, GV, false);
|
2020-10-15 22:57:21 +02:00
|
|
|
if (NeedQuotes)
|
|
|
|
OS << "\"";
|
2018-01-20 01:28:02 +01:00
|
|
|
}
|
|
|
|
|