1//===- System.inc ---------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include <cstring>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <sys/utsname.h>
13#include <ctype.h>
14#include <cstdlib>
15#include <fcntl.h>
16#include <unistd.h>
17
18#include <llvm/ADT/StringRef.h>
19
20namespace mcld{
21namespace sys{
22
23char *strerror(int errnum)
24{
25  return std::strerror(errnum);
26}
27
28static std::string getOSVersion()
29{
30  struct utsname info;
31
32  if (uname(&info))
33    return "";
34
35  return info.release;
36}
37
38std::string getDefaultTargetTriple()
39{
40  llvm::StringRef TargetTripleString(MCLD_DEFAULT_TARGET_TRIPLE);
41  std::pair<llvm::StringRef, llvm::StringRef> ArchSplit = TargetTripleString.split('-');
42
43  // Normalize the arch, since the target triple may not actually match the target.
44  std::string Arch = ArchSplit.first;
45
46  std::string Triple(Arch);
47  Triple += '-';
48  Triple += ArchSplit.second;
49
50  // Force i<N>86 to i386.
51  if (Triple[0] == 'i' && isdigit(Triple[1]) &&
52      Triple[2] == '8' && Triple[3] == '6')
53    Triple[1] = '3';
54
55  // On darwin, we want to update the version to match that of the
56  // target.
57  std::string::size_type DarwinDashIdx = Triple.find("-darwin");
58  if (DarwinDashIdx != std::string::npos) {
59    Triple.resize(DarwinDashIdx + strlen("-darwin"));
60    Triple += getOSVersion();
61  }
62
63  return Triple;
64}
65
66int GetPageSize()
67{
68  return getpagesize();
69}
70
71/// random - generate a random number.
72long GetRandomNum()
73{
74  return ::random();
75}
76
77/// srandom - set the initial seed value for future calls to random().
78void SetRandomSeed(unsigned pSeed)
79{
80  ::srandom(pSeed);
81}
82
83} // namespace of sys
84} // namespace of mcld
85
86