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