1/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/crypto.h>
16
17#include "internal.h"
18
19#if !defined(OPENSSL_NO_ASM) && \
20    (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
21/* x86 and x86_64 need to record the result of a cpuid call for the asm to work
22 * correctly, unless compiled without asm code. */
23#define NEED_CPUID
24
25#else
26
27/* Otherwise, don't emit a static initialiser. */
28
29#if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
30#define BORINGSSL_NO_STATIC_INITIALIZER
31#endif
32
33#endif  /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64) */
34
35#if defined(OPENSSL_WINDOWS)
36#define OPENSSL_CDECL __cdecl
37#else
38#define OPENSSL_CDECL
39#endif
40
41#if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
42#if !defined(OPENSSL_WINDOWS)
43static void do_library_init(void) __attribute__ ((constructor));
44#else
45#pragma section(".CRT$XCU", read)
46static void __cdecl do_library_init(void);
47__declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
48    do_library_init;
49#endif
50#endif  /* !BORINGSSL_NO_STATIC_INITIALIZER */
51
52/* do_library_init is the actual initialization function. If
53 * BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
54 * initializer. Otherwise, it is called by CRYPTO_library_init. */
55static void OPENSSL_CDECL do_library_init(void) {
56#if defined(NEED_CPUID)
57  OPENSSL_cpuid_setup();
58#endif
59}
60
61void CRYPTO_library_init(void) {
62  /* TODO(davidben): It would be tidier if this build knob could be replaced
63   * with an internal lazy-init mechanism that would handle things correctly
64   * in-library. */
65#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
66  do_library_init();
67#endif
68}
69